ASPX templates
Version 4.x > ASPX templates > Databinding-problem? View modes: 
User avatar
Member
Member
rookie07 - 9/8/2009 6:52:49 AM
   
Databinding-problem?
hi there,

i've just downloaded the new 4.1-version which is fabulous but now i stuck with a problem:

i've created an inline control which should manipulate a newsletter-adress-list. so i've used the generic asp.net gridview. filtering, paging, updating and deleting worked fine but now i had to integrate a listbox in one cell to be updated.

first of course i've tried this in a seperat demo project but as soon as i want to convert it in an inlinecontrol it doesn't work (a webpart neither).

the gridview represents the table: kontakte (contacts), as soon as i klick on update the column: anrede (title) converts into a listbox presenting the choice oer titles like: male, female aso...
if i want the changes to be updated and click on the button, i get following error:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Source Error:

[No relevant source lines]


Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\promontamag\6c85709b\33cafe95\App_Web_w8puwbdu.4.cs Line: 0

Stack Trace:

[InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.]
System.Web.UI.Page.GetDataItem() +2638766
ASP.cmsinlinecontrols_promontam_admin_kontakte_ascx.__DataBinding__control10(Object sender, EventArgs e) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\promontamag\6c85709b\33cafe95\App_Web_w8puwbdu.4.cs:0
System.Web.UI.Control.OnDataBinding(EventArgs e) +99
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +14
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.ListBox.LoadPostData(String postDataKey, NameValueCollection postCollection) +65
System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +13
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +346
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1434


Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082


Here is the code i use:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="50"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="btnDelete" Text="Löschen" runat="server" OnClientClick="return confirm('Soll dieser Datensatz wirklich gelöscht werden?');" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ButtonType="Link" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:TemplateField HeaderText="anrede" SortExpression="anrede">
<EditItemTemplate>
<asp:ListBox ID="lbAnrede" runat="server" DataSourceID="SqlDataSource2"
Rows="1" SelectionMode="Single" SelectedValue='<%# Bind("anrede") %>'
DataTextField="bezeichnung" DataValueField="id" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("anrede") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="vorname" HeaderText="vorname" SortExpression="vorname" />
<asp:BoundField DataField="nachname" HeaderText="nachname" SortExpression="nachname" />
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
<asp:CheckBoxField DataField="newsletterabo" HeaderText="newsletterabo" SortExpression="newsletterabo" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CMSConnectionString %>"
DeleteCommand="DELETE FROM [kontakte] WHERE [id] = @id"
InsertCommand="INSERT INTO [kontakte] ([anrede], [vorname], [nachname], [email], [newsletterabo]) VALUES (@anrede, @vorname, @nachname, @email, @newsletterabo)"
SelectCommand="SELECT [id], [anrede], [vorname], [nachname], [email], [newsletterabo] FROM [kontakte]"
UpdateCommand="UPDATE [kontakte] SET [anrede] = @anrede, [vorname] = @vorname, [nachname] = @nachname, [email] = @email, [newsletterabo] = @newsletterabo WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="anrede" Type="Int32" />
<asp:Parameter Name="vorname" Type="String" />
<asp:Parameter Name="nachname" Type="String" />
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="newsletterabo" Type="Boolean" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="anrede" Type="Int32" />
<asp:Parameter Name="vorname" Type="String" />
<asp:Parameter Name="nachname" Type="String" />
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="newsletterabo" Type="Boolean" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:CMSConnectionString %>"
SelectCommand="SELECT [id], [bezeichnung] FROM [anrede]">
</asp:SqlDataSource>


any suggestions?
thank you a lot!

User avatar
Member
Member
rookie07 - 9/10/2009 6:56:52 AM
   
RE:Databinding-problem?
ok. it seems i worked arround the problem. there is a second postback which seems to bring the problems (binding null-value)...hadn't enough time to do deep debugging...

anyway with the help of: http://social.msdn.microsoft.com/forums/en-US/Vsexpressinstall/thread/b51a089d-8d85-4af9-8393-bdcb22707de0/

is somehow solved the problem like this:

<div class="cmxform">
<table>
<tr>
<td><asp:Label id="lblVorname" runat="server" AssociatedControlID="tbVorname" Text="Vorname: " /> <asp:TextBox ID="tbVorname" runat="server" Text="" /></td>
<td><asp:Label id="lblNachname" runat="server" AssociatedControlID="tbNachname" Text="Nachname: " /><asp:TextBox ID="tbNachname" runat="server" Text="" /></td>
<td><asp:Label id="lblEmail" runat="server" AssociatedControlID="tbEmail" Text="Email: " /><asp:TextBox ID="tbEmail" runat="server" Text="" /></td>
<td><asp:Button ID="btnSuche" runat="server" Text="suchen" OnClick="btnSuche_OnClick" /></td>
</tr>
</table>
<asp:GridView ID="GridViewKontakte" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSourceKontakte" PageSize="50" OnRowUpdating="GridViewKontakte_OnRowUpdating">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="btnDelete" Text="Löschen" runat="server" OnClientClick="return confirm('Soll dieser Datensatz wirklich gelöscht werden?');" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:TemplateField HeaderText="anrede" SortExpression="anrede">
<ItemTemplate>
<asp:Label ID="lbAnrede" runat="server" Text='<%#Bind("anrede") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="cbAnrede" runat="server"
DataSourceID="SqlDataSourceAnrede" DataTextField="bezeichnung" DataValueField="ID"
SelectedValue='<%# BindEx(DataBinder.Eval(Container.DataItem,"anrede")) %>'
AppendDataBoundItems="False" AutoPostBack="true" OnSelectedIndexChanged="cbAnrede_OnSelectedIndexChanged" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="vorname" HeaderText="vorname" SortExpression="vorname" />
<asp:BoundField DataField="nachname" HeaderText="nachname" SortExpression="nachname" />
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
<asp:CheckBoxField DataField="newsletterabo" HeaderText="newsletterabo" SortExpression="newsletterabo" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceKontakte" runat="server"
ConnectionString="<%$ ConnectionStrings:CMSConnectionString %>"
DeleteCommand="DELETE FROM [kontakte] WHERE [id] = @original_id"
InsertCommand="INSERT INTO [kontakte] ([anrede], [vorname], [nachname], [email], [newsletterabo]) VALUES (@anrede, @vorname, @nachname, @email, @newsletterabo)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [id], [anrede], [vorname], [nachname], [email], [newsletterabo] FROM [kontakte] WHERE (([email] LIKE @email + '%') AND ([nachname] LIKE @nachname + '%') AND ([vorname] LIKE @vorname + '%'))"
UpdateCommand="UPDATE [kontakte] SET [anrede] = @anrede, [vorname] = @vorname, [nachname] = @nachname, [email] = @email, [newsletterabo] = @newsletterabo WHERE [id] = @original_id">
<SelectParameters>
<asp:ControlParameter ControlID="tbEmail" Name="email" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="tbNachname" Name="nachname" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="tbVorname" Name="vorname" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="original_id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="anrede" Type="Int32" />
<asp:Parameter Name="vorname" Type="String" />
<asp:Parameter Name="nachname" Type="String" />
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="newsletterabo" Type="Boolean" />
<asp:Parameter Name="original_id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="anrede" Type="Int32" />
<asp:Parameter Name="vorname" Type="String" />
<asp:Parameter Name="nachname" Type="String" />
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="newsletterabo" Type="Boolean" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceAnrede" runat="server" ConnectionString="<%$ ConnectionStrings:CMSConnectionString %>"
SelectCommand="SELECT [id], [bezeichnung] FROM [anrede] ORDER BY [id]">
</asp:SqlDataSource>
</div>


and:

public partial class CMSInlineControls_PromontAM_admin_kontakte : CMS.ExtendedControls.InlineUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session.Remove("cbAnredeSelection");
}

if (!HttpContext.Current.User.Identity.IsAuthenticated)
Response.Redirect("~/SERVICE/ADMIN.aspx");
}

protected void btnSuche_OnClick(object sender, EventArgs e)
{
GridViewKontakte.DataBind();
}

protected void GridViewKontakte_OnRowUpdating(Object sender, GridViewUpdateEventArgs e)
{
e.NewValues["anrede"] = Session["cbAnredeSelection"].ToString();
}

protected void cbAnrede_OnSelectedIndexChanged(object sender, EventArgs e)
{
GetSelectedAnrede();
}

private void GetSelectedAnrede()
{
foreach (GridViewRow r in this.GridViewKontakte.Rows)
{
if (r.RowState == DataControlRowState.Edit || r.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))
{
DropDownList dl = (DropDownList)r.FindControl("cbAnrede");
if (dl != null)
{
Session["cbAnredeSelection"] = dl.SelectedValue;
}
break;
}
}
}

public string BindEx(object input)
{
string ret = "";
if (input != null)
{
ret = input.ToString();
if (Session["cbAnredeSelection"] == null)
{
Session["cbAnredeSelection"] = ret;
}
else if (ret != Session["cbAnredeSelection"].ToString() && !string.IsNullOrEmpty(Session["cbAnredeSelection"].ToString()))
{
ret = Session["cbAnredeSelection"].ToString();
}
}
else
{
ret = Session["cbAnredeSelection"].ToString();
}
return ret;
}
}