Whnever the control (whichever event) changes the data to the datasource, call the update() method of the updatepanel which contains the Dropdownlist. You might also have to call the Select event of the DataSource and rebind it to the DropDownList.
codeasp:
Whnever the control (whichever event) changes the data to the datasource, call the update() method of the updatepanel which contains the Dropdownlist. You might also have to call the Select event of the DataSource and rebind it to the DropDownList.
Thanks for your response. Greatly appreciate it.
Using just the update() method did not work. I am unable to rebind the datasource to the dropdown since the dropdown is inside a <InsertItemTemplate></InsertItemTemplate> and the codebehind is unable to find the control and throws a "Control does not exist in the current context" error.
Any other way around this?
Regards,
Nicky
Very basic example to show how it can be done. I have kept it very simple and haven't used updatepanel etc.
<asp:FormView ID="FormView1" runat="Server" Width="100%" OnDataBound="FormView1_DataBound"> <ItemTemplate><%#Eval("userName")%> </ItemTemplate> <InsertItemTemplate> <asp:DropDownList ID="UsersDDL" runat="server" DataTextField="UserName" DataValueField="UserID" /> </InsertItemTemplate> </asp:FormView> <asp:Button ID="AddNew" runat="server" Text="Add" OnClick="AddNew_Click" />
The code - behind:
Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)If Not IsPostBackThen Dim tableAs Data.DataTable = CreateSource() FormView1.ChangeMode(FormViewMode.ReadOnly) ViewState("DS") = table FormView1.DataSource = table FormView1.DataBind()End If End Sub Private Function CreateSource()As Data.DataTableDim tableAs New Data.DataTable table.Columns.Add(New Data.DataColumn("UserID",GetType(Integer))) table.Columns.Add(New Data.DataColumn("UserName",GetType(String))) table.Columns.Add(New Data.DataColumn("Age",GetType(String)))' Add dataDim rowAs Data.DataRowFor iAs Integer = 1To 10 row = table.NewRow() row(0) = i row(1) ="User Name" & i' Initialize the random-number generator. Randomize()' Generate random value between 1 and 99.Dim ageAs Integer =CInt(Int((99 * Rnd()) + 1)) row(2) = age table.Rows.Add(row)Next Return tableEnd Function Protected Sub FormView1_DataBound(ByVal senderAs Object,ByVal eAs System.EventArgs)If FormView1.CurrentMode = FormViewMode.InsertThen Dim dlAs DropDownList =CType(FormView1.FindControl("UsersDDL"), DropDownList)If dl IsNotNothing Then Dim tableAs Data.DataTable =CType(ViewState("DS"), Data.DataTable) dl.DataSource = table dl.DataBind()End If End If End Sub Protected Sub AddNew_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Dim tableAs Data.DataTable =CType(ViewState("DS"), Data.DataTable)If table IsNotNothing Then table = CreateSource()Dim rowAs Data.DataRow = table.NewRow() row(0) = 99 row(1) ="New User Name" row(2) = 89 table.Rows.Add(row) ViewState("DS") = table FormView1.ChangeMode(FormViewMode.Insert)End Sub
Thank you so much for your response.
My DropDownList is built like so:
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="userDataSource"
DataTextField="description" DataValueField="code" SelectedValue='<%# Bind("userCode") %>'>
</asp:DropDownList>
</InsertItemTemplate>
I tried the method you illustrated in your mail but when I call the ddl.DataBind() it again gives me the error "Databinding methods such as Eval(), XPath() and Bind() can only be used in the context of a databound control". What am I doing wrong?
Thanks much,
Nicky
Thank you, codeasp!
Your input really helped me solve the issue. I called the update method inside the control event that was making the changes and then performed the databind. The thing I was doing wrong was calling the update function of the updatepanel and then the databind. But now its solved. All thanks to you.
Greatly appreciate your help.
Thanks a ton.
Nicky
No comments:
Post a Comment