Here's how I got it working, this is the codebehind for my usercontrol:
private void Page_Load(object sender, EventArgs e) {
if(!((WebForm1)Page).IsControlReloaded){
BindGrid();
}
}
private void BindGrid() {
DataTable dt = new DataTable();
dt.Columns.Add("BillName");
dt.Columns.Add("BillValue");
for (int i = 0; i < 5; i++){
DataRow dr = dt.NewRow();
dr[0] = 1*(i + 1);
dr[1] = 3*(i + 1);
dt.Rows.Add(dr);
}
grdUpload.DataSource = dt;
grdUpload.DataBind();
}
protected void ddlMonthlyRecurring_EditCommand(object sender,
DataListCommandEventArgs e) {
grdUpload.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
protected void ddlMonthlyRecurring_UpdateCommand(object sender,
DataListCommandEventArgs e) {
TextBox Label_BillName =
(TextBox)e.Item.FindControl("Label_BillName");
//do whatever
}
Now, the only tricky part is the code in the Page_Load. As you can see, it
used the isControlReloaded of the Page to see if this is an internal
postback (you can't simply use Page.IsPostBack because then it won't work
the first time around). This is what the WebForm1 page codebehind looks
like:
public bool IsControlReloaded = false;
protected LinkButton lnk;
protected PlaceHolder plc;
private void Page_Load(object sender, EventArgs e) {
string c = (string)ViewState["c"];
if (c != null){
isControlReloaded = true;
plc.Controls.Add(Page.LoadControl(c));
}
}
private void lnk_Click(object sender, EventArgs e) {
ViewState.Add("c", "u1.ascx");
plc.Controls.Add(Page.LoadControl("u1.ascx"));
}
I hope that helps.
Karl
Bharat said:
Yeah update button is in the datalist like the edit button. Here is the
control description. When I place this directly on the form the update event
is raised. Only when I load this datalist control into a placeholder all
problems rise. If I don't rebind on every trip to server the control all
together dissapears.
<asp:datalist id="ddlMonthlyRecurring" runat="server" Font-Size="12pt"
BorderColor="black" BorderWidth="2"
GridLines="Both" CellPadding="5" CellSpacing="0" Font-Name="Verdana"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="aqua" OnEditCommand="ddlMonthlyRecurring_EditCommand"
OnUpdateCommand="ddlMonthlyRecurring_UpdateCommand"
OnCancelCommand="ddlMonthlyRecurring_CancelCommand"
Width="500px">
<HeaderTemplate>
Monthly Recurring Bills
</HeaderTemplate>
<EditItemStyle Font-Size="12px" BackColor="LemonChiffon"></EditItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemTemplate>
<asp:LinkButton id="button1" runat="server" Text="Edit" Width="100px"
CommandName="edit"></asp:LinkButton>
<asp:Label id=lblBillName runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "BillName") %>' Width="150px">
</asp:Label>
<asp:Label id=lblBillValue runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "BillValue") %>' Width="150px">
</asp:Label>
</ItemTemplate>
<HeaderStyle Font-Size="16px" BackColor="#AAAADD"></HeaderStyle>
<EditItemTemplate>
<asp:Label id="Textbox1" runat="server" Text='Bill Name:'></asp:Label>
<asp:TextBox id="Label_BillName" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "BillName") %>'>
</asp:TextBox>
<BR>
<BR>
<asp:Label id="Label_BillValue" runat="server" Text='Bill
Value:'></asp:Label>
<asp:TextBox id=Text1 runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "BillValue") %>'>
</asp:TextBox>
<BR>
<BR>
<asp:LinkButton id="button2" runat="server" Text="Update"
CommandName="update" Width="30px"></asp:LinkButton>
<asp:LinkButton id="button3" runat="server" Text="Cancel"
CommandName="cancel" Width="30px"></asp:LinkButton>
</EditItemTemplate>
</asp:datalist>
Thanks
Bharat