Get value on edititemtemplate from codebehind

I have a detailed view where I get a couple of data from a membership profile and I show it in a detailed view ... this works fine:

<ItemTemplate> <asp:label ID="FirstName" runat="server" /> </ItemTemplate> 

But when I click the edit button, nothing is displayed on the field. This is what I do in the Edit editor:

I call ItemUpdating as follows:

  protected void DetailsView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e) { //I get my memberprofle here MemberProfile memberp = MemberProfile.GetuserProfile(data); MembershipUser myuser = Membership.GetUser() Label labelfName = DetailsView1.FindControl("FirstName") as Label; labelfName.Text = memberp.fName; } 

Should I use Itemupdated instead? Or is there another method I have to call when the edit button is pressed that will fill the firstname field when editing? Also, the reason I save it as "LABEL" (usually a text block) in edit mode is because this field should be read-only.

+4
source share
2 answers

The ItemUpdating event does not fire when entering edit mode. You must use the DataBound event to set the correct required text value.

If necessary, you can set the CurrentMode property of the DetailsView element to see if you are editing or displaying.

The result is as follows:

 protected void DetailsView1_DataBound(object sender, EventArgs e) { Label l = DetailsView1.FindControl("FirstName") as Label; if (DetailsView1.CurrentMode == DetailsViewMode.Edit) { //obtained from your sample MemberProfile memberp = MemberProfile.GetuserProfile(data); MembershipUser myuser = Membership.GetUser() l.Text = memberp.fName; } else { l.Text = "Another text or nothing"; } } 

Be sure to define the DataBound event in the Detailsview1 control.

SIGN . This may depend on the data binding mode. If so, let me know and attach an example.

+1
source

Add the RowUpdating and RowEditing event to the gridview.

http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html

0
source

All Articles