I have a gridview that adds rows in the footertemplate that works fine. I want to add RowEditing and RowDeleting to the grid.
I added the RowEditing method but when I click the 'Edit' Button, the add row is displayed in the footer as if it is the "Add Row' command.
When I created a prototype of a grid, I had a column just for editing and it was a commandfield. WHen you click the button, the row would change so that all of the fields were editable. How can I do that when the field is an ItemTemplate field?
This is my markup:
<asp:GridView ID="MappingGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False" Caption="Enrollment Mapping Information" CaptionAlign="Top"
CssClass="grid" HorizontalAlign="Left" ShowFooter="True" AllowPaging="True" PageSize="4" ShowHeaderWhenEmpty="true" OnPageIndexChanging="MappingGridView_PageIndexChanging"
OnRowDataBound="MappingGridView_RowDataBound" OnRowCommand="MappingGridView_RowCommand" Width="1051px" OnRowEditing="MappingGridView_RowEditing">
<Columns>
<asp:TemplateField HeaderText="MappingID" SortExpression="mgvMappingID">
<ItemTemplate>
<asp:Label ID="mgvLblMappingID" runat="server" Text='<%# Bind("EnrollmentMappingID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SectionID">
<EditItemTemplate>
<asp:TextBox ID="mgvDdlSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mgvInsertSectionID" runat="server" ReadOnly="true" Text="" Width="90%"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SchoologyCourseID" SortExpression="mgvSchoologyCourseID">
<EditItemTemplate>
<asp:TextBox ID="mgvEditSchoologyCourseID" runat="server" Text='<%# Bind("SchoologyCourseID") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValEditSchoologyCourseID" ControlToValidate="mgvEditSchoologyCourseID" runat="server"
ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValEditSchoologyCourseID" ControlToValidate="mgvEditSchoologyCourseID" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblSchoologyCourseID" runat="server" Text='<%# Bind("SchoologyCourseID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mgvInsertSchoologyCourseID" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValSchoologyCourseID" ControlToValidate="mgvInsertSchoologyCourseID" runat="server"
ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValSchoologyCourseID" ControlToValidate="mgvInsertSchoologyCourseID" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CECityActivityID" SortExpression="mgvCECityActivityID">
<EditItemTemplate>
<asp:TextBox ID="mgvEditCECityActivityID" runat="server" Text='<%# Bind("CECityActivityID") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValEditCECityID" ControlToValidate="mgvEditCECityActivityID" runat="server"
ErrorMessage="Enter 0-9, A-F, and hyphens. Maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblCECityActivityID" runat="server" Text='<%# Bind("CECityActivityID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mgvInsertCECityActivityID" runat="server" Width="90%"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValCECityID" ControlToValidate="mgvInsertCECityActivityID" runat="server"
ErrorMessage="Enter only 0-9, A-F, and hyphens; maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="mgvIsActive">
<EditItemTemplate>
<asp:DropDownList ID="mgvEditIsActive" runat="server" Text='<%# Bind("IsActive") %>'>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblIsActive" runat="server" Text='<%# Bind("IsActive") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="mgvInsertIsActive" runat="server" Width="90%">
<asp:ListItem Selected="True">Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Max. Enrollment">
<EditItemTemplate>
<asp:TextBox ID="mgvEditMaxEnrollment" runat="server" Text='<%# Bind("MaxEnrollment") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValEditMaxEnroll" ControlToValidate="mgvEditMaxEnrollment" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblMaxEnrollment" runat="server" Text='<%# Bind("MaxEnrollment") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mgvInsertMaxEnrollment" runat="server" Width="90%"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValMaxEnroll" ControlToValidate="mgvInsertMaxEnrollment" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="mgvImgUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:Button>
<asp:Button ID="mgvImgCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:Button>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="mgvEditButton" runat="server" CausesValidation="True" ValidationGroup="MappingGrid" CommandName="Edit" Text="Edit" Width="40%"></asp:Button>
<asp:Button ID="mgvDeleteButton" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" Width="40%"></asp:Button>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:Button ID="mgvBtnAdd" runat="server" CommandName="Add" Text="Add Mapping" Width="90%" CausesValidation="true" ValidationGroup="MappingGrid" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<tr>
<td colspan="11" style="text-align:center;">
No mapping data was found for this section. Mappings can be added to this section by clicking the 'Add Mapping' Button.
</td>
</tr>
<tr>
<td></td>
<td><asp:TextBox ID="mgvInsertSectionID" runat="server" ReadOnly="true" Width="90%"></asp:TextBox></td>
<td><asp:TextBox ID="mgvInsertSchoologyCourseID" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValSchoologyCourseIDEmpty" ControlToValidate="mgvInsertSchoologyCourseID" runat="server"
ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValSchoologyCourseIDEmpty" ControlToValidate="mgvInsertSchoologyCourseID" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:TextBox ID="mgvInsertCECityActivityID" runat="server" Width="90%"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValCECityIDEmpty" ControlToValidate="mgvInsertCECityActivityID" runat="server"
ErrorMessage="Enter only 0-9, A-F, and hyphens; maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:DropDownList ID="mgvInsertIsActive" runat="server" Width="90%">
<asp:ListItem Selected="True">Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList></td>
<td><asp:TextBox ID="mgvInsertMaxEnrollment" runat="server" Width="90%"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValMaxEnrollEmpty" ControlToValidate="mgvInsertMaxEnrollment" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:Button ID="mgvBtnAddEmpty" runat="server" CommandName="Add" Text="Add Mapping" Width="90%" CausesValidation="true"
ValidationGroup="MappingGrid" />
</td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
This is my code behind:
protected void MappingGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
MappingGridView.EditIndex = e.NewEditIndex;
populateEnrollMappingGrid();
}
When I click the edit button, the RowEditing method is not called and the footer row displays the insert action.
How do I get the Edit button to make the row clicked editable?
UPDATED INFORMATION Page_Load
This is the Page_Load Method:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
//Get the Environment Setting to determine the database to access
txtBoxEnvironment.Text = CurrentEnvironment;
DAL.setCurrentEnvironment(CurrentEnvironment);
//Get All Section information from database
CSectionInfo.getData();
//Get All Mapping informatio from database
CEnrollMappingInfo.getData();
//Populate the Mapping grid
MappingGridView.DataSource = CEnrollMappingInfo.getInitialData();
//Populate the Drop Down Section List
populateDropDownSectionList(CEnrollMappingInfo.SelectedSectionID);
//Grid must be bound after the Dropdown List is set so the value in the footer can be determined
MappingGridView.DataBind();
//Populate the Section grid
SectionGridView.DataSource = CSectionInfo.getDataUsingSectionID(CEnrollMappingInfo.SelectedSectionID);
//Get Course information from database
populateCourseGrid();
//Populate the Drop Down Course List
populateDropDownCourseList(CSectionInfo.SelectedCourseID);
//Grid must be bound after the Dropdown List is set so the value in the footer can be determined
SectionGridView.DataBind();
}
}
catch (Exception ex)
{
logger.ErrorException(ex.Message, ex);
Response.Redirect("~/Error.aspx");
}
}
Thanks.
Just a guess, you are databinding the GridView on every postback.
You have a Page_Load similar to:
protected void Page_Load(object sender, EventArgs e)
{
populateEnrollMappingGrid(); // a method where you assign the DataSource and call grid.DataBind()
}
Use the IsPostBack property:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
populateEnrollMappingGrid();
}
I corrected the problem...
In my markup, I set the Edit Button CausesValidation=true and ValidationGroup-MappingGrid which is what the insert validation is in the footer. To correct, I set the CausesValidation to false and removed the ValidationGroup.
I set these values in the Update Button field.
Work now!
Thanks.
Related
I am trying to delete rows from ASP.NET GridView and for demo purpose, I am trying to retrieve names from the rows right now. In the ItemTemplate of the GridView, I've included Label that has id as well name to show. Finally added Button control in the GridView to delete specific rows and tried the following:
<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("ProductId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnShow" runat="server" Text="Button" OnClick="btnShow_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
In the code-behind, I've iterated the GridView with a loop to get the individual values, say name of the row as follows:
protected void btnShow_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdData.Rows)
{
string val = ((Label)row.FindControl("lblName")).Text;
lblMsg.Text = val;
}
}
But unfortunately I get the last name every time I click any row button and here is the screen-shot that I am trying:
Whenever I click any button, it shows the last name Ice-Cream every time. Anything that I missed here?
<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("ProductId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnShow" runat="server" Text="Button" OnClick="btnShow_Click" CommandArgument='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
protected void btnShow_Click(object sender, EventArgs e)
{
lblMsg.Text = (sender as Button).CommandArgument;
}
What I need: TARGET or ACTUAL on first line and rest on next line.
What I have till now
I have this gridview templatefield in asp.net
<asp:TemplateField HeaderStyle-CssClass="headerStyle" HeaderText="TARGET Approval Date" FooterStyle-CssClass="alternateStyler" ItemStyle-CssClass="alternateStyler" ItemStyle-Width="8%" ItemStyle-VerticalAlign="Top" FooterStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="dtTgtApprDate" CssClass="Datetext" Width="98%" Height="23px" runat="server" Text='<%# Eval("Phase2")%>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="dtTgtApprDate2" CssClass="Datetext" Width="98%" Height="23px" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="headerStyle" HeaderText="ACTUAL Approval Date" FooterStyle-CssClass="alternateStyler" ItemStyle-CssClass="alternateStyler" ItemStyle-Width="8%" ItemStyle-VerticalAlign="Top" FooterStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="dtActApprDate" CssClass="Datetext" Width="98%" Height="23px" runat="server" Text='<%# Eval("Phase3")%>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="dtActApprDate2" CssClass="Datetext" Width="98%" Height="23px" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
I have put this css for HeaderStyle-CssClass="headerStyle"
.headerStyle {
text-justify:distribute;
}
Since the header is HTML also, you can just put a <br> in it.
<asp:TemplateField HeaderText="TARGET<br>ApprovalDate">
i can't get the value of textbox from the footer row of gridview
<asp:GridView ID="GridView1" runat="server" Width="1214px"
AutoGenerateColumns="False" ShowFooter="true"
OnRowCommand="GridView1_RowCommand"
<Columns>
<asp:TemplateField HeaderText="Insert">
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<EditItemTemplate>
<asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSName" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and the code behind is........
i can't get the value of textbox from the footer row of gridview
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName==("AddNew"))
{
TextBox txtName =(TextBox) GridView1.FooterRow.FindControl("txtSName");
string strName=txtName.Text; //strName is Empty while i m enters data into the textbox txtSName
}
Posting this comment to help others who may encounter the same issue.
Make sure the grid is not loading again on post back.
code in the page load should look like this
if (!IsPostBack)
{
LoadGrid();
}
Your mark up is biting you.You are having two footer templates within a template field.
Do this
<asp:TemplateField HeaderText="Insert">
<ItemTemplate>
<asp:ImageButton ID="EditImageButton" runat="server" CommandName="Edit"
ImageUrl="~/images/Edit.png"Style="height: 16px" ToolTip="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete"
CausesValidation="false" OnClientClick="return confirm('Delete.Are you sure you want to delete?')"
ImageUrl="~/images/DeleteTS.png" Text="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="AddNewImgBtn" runat="server" ImageUrl="~/images/saveHS.png"
ToolTip="Add New" AlternateText="Add New" Width="16px" Height="16px"
CommandArgument="InsertNew" ImageAlign="AbsMiddle" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<EditItemTemplate>
<asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSName" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And then you can gracefully locate your footer row.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandArgument=="InsertNew")
{
GridView testGrid=(Gridview)sender;
TextBox txtName =(TextBox)testGrid.FooterRow.FindControl("txtSName");
string strName=txtName.Text;
}
}
See working example Example
I have an ajax tab which inside has a grid, on this grid there is a check box where the user has the ability to check it and assign it to a technician, but for some reason im unable to reference the control, the grid has a total of 4 rows so there is data there, this is my code which is run of a button click
protected void btnAllocate_click(object sender, EventArgs e)
{
foreach (System.Web.UI.Control s in tbHappyCall.Controls)
{
foreach (GridViewRow Row in gvHappyCall.Rows)
{
CheckBox chkAllocate = (CheckBox)Row.FindControl("chkAllocate");
if (chkAllocate.Checked)
{
HyperLink lblOrderID = (HyperLink)Row.FindControl("hyperOrderID");
HappyCallManager objHappyCallManager = new HappyCallManager();
objHappyCallManager.HappyCallAllocated(Convert.ToInt64(lblOrderID.Text), objWebuser.ShortAbbr, ddlAllocateTo.SelectedValue);
//Update database with person details thats are assigned to the Welcome Call
}
}
}
}
when it goes on to the Foreach gridviewrow etc the count is 1 even though there is 4 rows displaying information ?
Can any one shed any light on this? or even better a solution?
Thank you for your time.
UPdate
<ajaxToolkit:TabPanel ID="tbHappyCall" runat="server" HeaderText="Welcome Calls" TabIndex="10">
<ContentTemplate>
<%--OnRowDataBound="gvConfirmOrder_rowDataBound"--%>
<div id="divHappyCall">
<asp:GridView ID="gvHappyCall" runat="server" AutoGenerateColumns="false" class="tablesorter"
EmptyDataText="There is no Record to display." DataKeyNames="OrderID" EnableViewState="false"
OnRowDataBound="gvHappyCall_RowDataBound">
<AlternatingRowStyle CssClass="NormalTextVNC" BackColor="#E2E9E7"></AlternatingRowStyle>
<Columns>
<asp:TemplateField HeaderText="Account Manager">
<ItemTemplate>
<asp:Label ID="lblAccountManager" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AccountManager") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<asp:HyperLink ID="hyperOrderID" runat="server" ForeColor="White" NavigateUrl='<%#Eval("OrderGuid","/Documents/HappyCall.aspx?OrderID={0}") %>'
Text='<%#Eval("OrderID") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:Label ID="lblCustomerName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name">
<ItemTemplate>
<asp:Label ID="lblCompanyName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile Number">
<ItemTemplate>
<asp:Label ID="lblMobileNumber" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MobileNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dispatch Date">
<ItemTemplate>
<asp:Label ID="lblConnectionDate" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DespatchDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField headertext="Date Called" dataformatstring="{0:dd/MM/yyyy}" datafield="EditedDate" HeaderStyle-Width="100px" />
<asp:TemplateField HeaderText="Allocated To">
<ItemTemplate>
<asp:Label ID="lblAllocatedTo" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AllocatedTo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:TemplateField HeaderText="Last Action">
<ItemTemplate>
<asp:Label ID="lblLAstAction" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LastAction") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Check">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkAssignWelcomeCall" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:DropDownList runat="server" ID="ddlAllocateTo" CssClass="floatright">
<asp:ListItem>Name of Admin to Allocate</asp:ListItem>
</asp:DropDownList>
<div class="WhiteSpace"></div>
<asp:Button runat="server" ID="btnAllocate" class="floatright" Text="Allocate" OnClick="btnAllocate_click" />
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
For clarification: you don't need to iterate the control collection of your TabPanel to find your GridView, it is available directly and don't confuse it's Controls.Count with the number of GridView.Rows.Count.
Is ViewState disabled somewhere?
Edit: I see that the GridView's ViewState is disabled. I assume that it works when you enable it.
I have problem dealing with EditItemTemplate.
What I am trying to do is to update my TextBox txt_name but I can't get what the user ingresses And I get the old value instead at the code-behind.
Am I missing something?
FRONT CODE
<asp:GridView ID="GridView_account" runat="server" AutoGenerateColumns="false" ShowFooter="True"
OnRowCancelingEdit="GridView_account_RowCancelingEdit" OnRowEditing="GridView_account_RowEditing"
OnRowUpdating="GridView_account_RowUpdating" OnRowCommand="GridView_account_RowCommand"
OnRowDeleting="GridView_account_RowDeleting" OnSelectedIndexChanged="GridView_account_SelectedIndexChanged"
DataKeyNames="UID" Height="110px" Width="497px">
<Columns>
<asp:TemplateField HeaderText="UID" SortExpression="UID">
<ItemTemplate>
<asp:Label ID="label_accid" runat="server" Text='<%# Bind("UID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Name (required)" SortExpression="NyA">
<EditItemTemplate>
<asp:TextBox ID="txt_name" runat="server" Text='<%# Bind("NyA") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_newname" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="label_name" runat="server" Text='<%# Bind("NyA") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address (required)" SortExpression="eMail">
<EditItemTemplate>
<asp:TextBox ID="txt_email" runat="server" Text='<%# Bind("eMail") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_newemail" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="label_email" runat="server" Text='<%# Bind("eMail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton_update" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton_cancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
<asp:LinkButton ID="LinkButton_delete" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton_addnew" runat="server" CausesValidation="False" CommandName="AddNew"
Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton_edit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Manage Role" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="Selectbtn" runat="server" CausesValidation="False" CommandName="Select"
Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CODE BEHIND
protected void GridView_account_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
user u = Business.GetUser(((Label)GridView_account.Rows[e.RowIndex].FindControl("label_accid")).Text);
//HERE'S MY PROBLEM!
u.fullname = ((TextBox)GridView_account.Rows[e.RowIndex].FindControl("txt_name")).Text;
//txt_name is returning the old value and not the one the user has input recently.
Business.UpdateUser(u);
GridView_account.EditIndex = -1;
fillgridview();
}//
Are you checking if Page IsPostback when binding your grid? You need to not rebind it in the Page_Load if it is a post back, or else you will lose the new values, because the Page Init, Page Load events happen before other events, such as a Grid Updating event.
May be you are binding you grid on every page_load event. If so, you need something like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
GridView_account.DataSource = "data source";
GridView_account.DataBind();
}
}
or instead of
if(!Page.IsPostBack)
you can use
if (GridView_account.EditIndex == -1)
this checks, if grid is in edit mode. If grid is not in edit mode, you can bind your grid with data source.