I am using a asp gridview with BoundFields and TemplatedFields.
<asp:GridView ID="RepairGrid" runat="server" CssClass="subitem w100p"
AutoGenerateColumns="False"
OnRowCommand="RepairGrid_SelectedIndexChanged" OnRowDataBound="RepairGrid_RowDataBound"
AllowSorting="True" OnSorting="SortGrid">
<RowStyle CssClass="itemRowStyle"/>
<AlternatingRowStyle CssClass="alternatingItemRowStyle"/>
<HeaderStyle CssClass="headerRowStyle"/>
<Columns>
<asp:TemplateField HeaderText="PDF" ItemStyle-CssClass="itemFieldStyle"
meta:resourcekey="ColumnPDF" HeaderStyle-CssClass="headerFieldStyle">
<ItemTemplate>
<asp:ImageButton ID="btnExportPDF" runat="server" CommandArgument='<%# Eval("RepairId") %>'
CommandName="ExportPDF" ImageUrl='<%# "~/App_Themes/" + Page.Theme + "/icons/16/icon_pdf.png" %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RepairId" SortExpression="RepairId" ItemStyle-CssClass="itemFieldStyle"
meta:resourcekey="ColumnRepairId" HeaderStyle-CssClass="headerFieldStyle" >
<ItemTemplate>
<asp:LinkButton ID="lnkRepairId" CommandArgument='<%# Eval("RepairId") %>' CommandName="Redirect"
runat="server" Text='<%# Eval("RepairId") %>'/>
<asp:Label ID="lblRepairId" runat="server" Text='<%# Eval("RepairId") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProblemCategory" SortExpression="InternalDescription" ItemStyle-CssClass="itemFieldStyle"
meta:resourcekey="repairItemText" HeaderStyle-CssClass="headerFieldStyle">
<ItemTemplate>
<asp:LinkButton ID="lnkProblemCategory" CommandArgument='<%# Eval("RepairId") %>' CommandName="Redirect"
runat="server"/>
<asp:Label ID="lblProblemCategory" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CreateDate" HeaderText="Status" SortExpression="CreateDate" ItemStyle-CssClass="itemFieldStyle"
meta:resourcekey="ColumnCreateDate" HeaderStyle-CssClass="headerFieldStyle"/>
<asp:BoundField DataField="CreateUser" HeaderText="Status" SortExpression="CreateUser" ItemStyle-CssClass="itemFieldStyle"
meta:resourcekey="ColumnCreateUser" HeaderStyle-CssClass="headerFieldStyle"/>
<asp:BoundField DataField="ChangeDate" HeaderText="Status" SortExpression="ChangeDate" ItemStyle-CssClass="itemFieldStyle"
meta:resourcekey="ColumnChangeDate" HeaderStyle-CssClass="headerFieldStyle"/>
<asp:BoundField DataField="ChangeUser" HeaderText="Status" SortExpression="ChangeUser" ItemStyle-CssClass="itemFieldStyle"
meta:resourcekey="ColumnChangeUser" HeaderStyle-CssClass="headerFieldStyle"/>
</Columns>
</asp:GridView>
I have managed to sort Boundfields and RepairId Templated Field correctly, but since my TemplatedField ProblemCategory does not have its text in the DataSource and will be filled like this
foreach (RepairDataSet.tms_v_Repair_RepairItemRow row in tblCategories)
{
RepairDataSet.tms_RepairItemDataTable repItemTbl = new RepairDataSet.tms_RepairItemDataTable();
repItemTbl = repairBo.GetRepairItem(row.RepairItemId);
if (repItemTbl.Count > 0)
{
lnkProblemCategory.Text += "<br />" + repItemTbl[0].TextKey;
}
else
{
lnkProblemCategory.Text += "<br />";
}
}
In the Grid_SelectedIndexChanged I have done special case for sort event args, in witch i define the DataSource, as it should be.
Is there any possibility to sort the TemplatedField by its text and not its value in DB?
Solution one: Sort Menually
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="LinkButtonEmpName" runat="server" Text="Employee Name" CommandName="Sort" CommandArgument="Employees">
</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="LabelEmployee" runat="server" Text='<%# Bind("Employees") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxEmployee" runat="server"/>
</FooterTemplate>
</asp:TemplateField >
Solution 2 Sort By Columnd:
<asp:TemplateField SortExpression="Status">
<HeaderTemplate>
<asp:LinkButton ID="lnkSort" runat="server" Text="Status" CommandName="Sort" CommandArgument="Status" />
</HeaderTemplate>
</asp:TemplateField>
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Sort"))
{
FilterExpression = e.CommandArgument.ToString() + " LIKE '%" + txtCaseNumber.Text + "%'";
BindGridView();
}
}
Solution 3 Sort with custom templates:
<asp:TemplateField HeaderText="Category" SortExpression="Category">
<ItemTemplate>A value</ItemTemplate>
</asp:TemplateField>
Related
I have a Gridview with some data. In that I have 5 rows of paging. Whenever I check on checkbox in first page and go to second page and again I come to first page. The checkbox check value gets disappear.
The value of checked is not retained. How to get it maintain the viewstate of checkbox. Please suggest
CODE:
<asp:GridView ID="grdDisplayCMMData" runat="server" AutoGenerateColumns="false" Width="100%" ShowHeaderWhenEmpty="true" CssClass="heavyTable table" EmptyDataText="No records to display"
AllowPaging="true" PageSize="2" OnPageIndexChanging="grdDisplayCMMData_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID_CMM" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SAP ID">
<ItemTemplate>
<asp:Label ID="lblSAP_ID_CMM" runat="server" Text='<%#Eval("SAP_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID OD CHANGE">
<ItemTemplate>
<asp:Label ID="lblID_OD_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("ID_OD_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID OD CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblID_OD_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("ID_OD_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RRH COUNT CHANGE">
<ItemTemplate>
<asp:Label ID="lblRRH_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("RRH_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RRH COUNT CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblRRH_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("RRH_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TENANCY COUNT CHANGE">
<ItemTemplate>
<asp:Label ID="lblTENANCY_COUNTCHANGE_CMM" runat="server" Text='<%#Eval("TENANCY_COUNTCHANGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TENANCY COUNT CHANGE DATE">
<ItemTemplate>
<asp:Label ID="lblTENANCY_CHANGEDDATE_CMM" runat="server" Text='<%#Eval("TENANCY_CHANGEDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATUS">
<ItemTemplate>
<asp:Label ID="lblSTATUS_CMM" runat="server" Text='<%#Eval("STATUS") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CREATED BY" Visible="false">
<ItemTemplate>
<asp:Label ID="lblCREATEDBY_CMM" runat="server" Text='<%#Eval("CREATED_BY") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:CheckBox ID="chkApprRejCMM" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
SERVER CODE
protected void grdDisplayCMMData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
BindCMMData();
grdDisplayCMMData.PageIndex = e.NewPageIndex;
grdDisplayCMMData.DataBind();
}
catch (Exception ex)
{
string strErrorMsg = ex.Message.ToString() + " " + "StackTrace :" + ex.StackTrace.ToString();
CommonDB.WriteLog("ERROR:" + strErrorMsg, ConfigurationManager.AppSettings["IPCOLO_LOG"].ToString());
}
}
Let me know if anything else is required
Microsoft provided example here.
check list is stored in view state as List.
To transfer data between pages you cannot use viewstate.
The options of transferring data between pages are Session storage, transferring via query string or storing the needed value into some kind of database.
Example for sessions storage at the most basic level:
On first page:
Session["CheckboxValue"] = chkSomeCheckbox.Checked;
On the second page:
bool isCheckboxChecked = Convert.ToBoolean(Session["CheckboxValue"])
my data is not showing after clicking the Edit button from the Gridview.
here is my GridView code
<asp:UpdatePanel ID ="panel1" runat="server">
<ContentTemplate>
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" AllowPaging="true" OnRowCommand="gv1_RowCommand" CellPadding="4" HeaderStyle-BackColor="CornflowerBlue" BorderWidth="5" BorderColor="CornflowerBlue" Width="100%" CssClass="table table-hover">
<Columns>
<asp:TemplateField HeaderText ="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Bind ("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Type">
<ItemTemplate>
<asp:Label ID="lbltype" runat="server" Text='<%#Bind ("ItemType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Model">
<ItemTemplate>
<asp:Label ID="lblModel" runat="server" Text='<%#Bind ("ItemModel") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Quantity">
<ItemTemplate>
<asp:Label ID="lblQuan" runat="server" Text='<%#Bind ("ItemQuantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Unit">
<ItemTemplate>
<asp:Label ID="lblUnit" runat="server" Text='<%#Bind ("ItemUnit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Target Date">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%#Bind ("ItemDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Description">
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%#Bind ("ItemDesc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Remarks">
<ItemTemplate>
<asp:Label ID="lblRem" runat="server" Text='<%#Bind ("ItemRemarks") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="editRecord" ControlStyle-CssClass="btn btn-info" ButtonType="Button" Text="Edit" HeaderText="Edit">
<ControlStyle CssClass="btn btn-info" />
</asp:ButtonField>
<%--<asp:TemplateField HeaderText ="Status">
<ItemTemplate>
<asp:Label ID="lblStat" runat="server" Text='<%#Bind ("ItemStatus") %>'></asp:Label>
<asp:DropDownList ID ="ddlStat" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>--%>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
and here is my code for .cs
int index = Convert.ToInt32(e.CommandArgument);
//if(e.CommandName.Equals("editRecord"))
//{
GridViewRow gvrow = gv1.Rows[index];
lblIDs.Text = HttpUtility.HtmlDecode(gvrow.Cells[0].Text).ToString() ;
lblitemTypes.Text = HttpUtility.HtmlDecode(gvrow.Cells[1].Text).ToString();
lblModels.Text = HttpUtility.HtmlDecode(gvrow.Cells[2].Text).ToString();
lblQuans.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
lblUnits.Text = HttpUtility.HtmlDecode(gvrow.Cells[4].Text).ToString();
lblDates.Text = HttpUtility.HtmlDecode(gvrow.Cells[5].Text).ToString();
lblDescs.Text = HttpUtility.HtmlDecode(gvrow.Cells[6].Text).ToString();
lblRemarkss.Text = HttpUtility.HtmlDecode(gvrow.Cells[7].Text).ToString();
//ddlStat.Text = HttpUtility.HtmlDecode(gvrow.Cells[8].Text).ToString();
lblResult.Visible = false;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(#"<script type='text/javascript'>");
sb.Append("$('#editModal').modal('show');");
sb.Append(#"</script>");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EditModalScript", sb.ToString(), false);
the page goes through but it doesnt show the data from the gridview, i tried to put a breakpoint and i see that it doesnt have anyvalue inside, what is my error? is the HttpUtility.HtmlDecode correct? how can i transfer the data to the modal for editing. thank you!
When you have an UpdatePanel warp your code and your code not work after a post back you need to do two thinks to find the issue.
Temporary Remove the UpdatePanel and repeat what you do and see if the problem is on your code behind.
place back the UpdatePanel and open the javascript console of your browser and check for javascript errors.
<asp:GridView Width="300px" RowStyle-Height="20px" HeaderStyle-Height="25px" DataKeyNames="Id" ID="bankAccounts_List" CssClass="table" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Sorting Handle" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<span class="handle"><img src="../Images/sortingHandler.jpg" height="10" /></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Number">
<ItemTemplate>
<asp:TextBox ID="grd_txtbankAccountNumber" Width="150" onblur="return OnBlurGridTextbox(this);" BorderStyle="None" BackColor="White" style="text-align:left;" runat="server" Text='<%# Eval("AccountNo") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Priority" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:TextBox ID="grd_txtPriority" Width="100" BorderStyle="None" BackColor="White" runat="server" style="text-align:center"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:LinkButton runat="server" ID="grd_linkbutton" OnClientClick="return EditGridButton(this)">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" >
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
<ItemTemplate>
<asp:Label ID="grd_lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSaveBankInformation" runat="server" Text="<%$Resources:Strings,save%>"
OnClick="btnSaveBankInformation_Click" />
protected void btnSaveBankInformation_Click(object sender, EventArgs e)
{
TextBox txtBankAccountNumber = bankAccounts_List.Rows[i].Cells[GetColumnIndexByName(bankAccounts_List.Rows[i], "Account Number")].FindControl("grd_txtbankAccountNumber") as TextBox;
}
What ever I change in any of the textbox, On save button I always gets the old binded textbox value not the New value I just entered.
Kindly Help me. and yes i am not binding GridView on PostBack again using !IsPostBack
if (!IsPostBack) //Bind grid only when IsPostBack is false
{
grd.DataSource = //data source ;
grd.DataBind(); // bind data
}
First of all the ASPcode, the problemdescription below.
<asp:GridView ID="GridViewContacts" runat="server" ForeColor="#333333" DataKeyNames="L_ID_CONTACT" AllowPaging="True" AllowSorting="True"
OnPageIndexChanging="GridViewContacts_PageIndexChanging" PageSize="25" AutoGenerateColumns="False" OnRowCommand="GV_Contacts_RowCommand" >
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandArgument="Edit" CommandName="Edit" Text="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonView" runat="server" CommandArgument="View" CommandName="View" Text="View">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="L_Name" runat="server" Text='<%# Eval("L_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Companydetails">
<ItemTemplate>
<asp:Label ID="L_Companydetails" runat="server" Text='<%# Eval("L_Companydetails") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMail">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False" HeaderText="ID_CONTACT" >
<ItemTemplate>
<asp:Label Visible="false" ID="L_ID_CONTACT" runat="server" Text='<%# Eval("L_ID_CONTACT") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<%-- //Stylesettings here--%>
</asp:GridView>
Okay, then in the CodeBehind I have a Select from my database, where i select the ID_Contact, the Name, the Companydetails, which can be 1 per Row only.
On the RowCreated event I get the UserID of the actual User, and I select all E-Mails the user has, can be 0-10 per row.
Now my problem is:
How can i insert Linkbuttons with the onClick-event in the description into this part of my code?
Like this:
<asp:TemplateField HeaderText="EMail">
<ItemTemplate>
<asp:LinkButton[i] runat="server" onClick="SendEmail">
</asp:Linkbutton[i]>
<asp:LinkButton[i] runat="server" onClick="SendEmail">
</asp:Linkbutton[i]>
</ItemTemplate>
</asp:TemplateField>
So i want to add those controlls with code into THIS TemplateField.
Is this possible ?
Thoughts i allready had:
This.GridViewContacs.Controlls.AddAt(index,Linkbutton)
But also no clue here how it should work.
Thanks in advance,
me
Easiest is to add a placeholder control to the ItemTemplate, as ItemTemplate has no ID.
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="emails" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
and then in RowDataBound event
if (e.Row.RowType == DataControlRowType.DataRow)
{
PlaceHolder emails = e.Row.FindControl("emails") as PlaceHolder;
if (emails != null)
{
LinkButton lbEmail = new LinkButton();
lbEmail.Text = "your text";
lbEmail.Click += new EventHandler(SendEmail);
emails.Controls.Add(lbEmail);
}
}
Of course, the example is simplified. You can easily extend it to your needs.
I have wired a gridSearchResults_RowUpdating event for everytime an update link button is clicked. What I want to do is loop through the row at the event rowindex and add the textbox value in the row cell to a string collection. I am having troubh accessing the textbox in the cell. If this doesn't make sense my code should help make things clearer.
HTML
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("jobTitle") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("jobTitle") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmailAddress" runat="server" Text='<%# Bind("EmailAddress") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Telephone Number" SortExpression="TelephoneNumber">
<ItemTemplate>
<asp:Label ID="lblTelephoneNumber" runat="server" Text='<%# Eval("TelephoneNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTelephoneNumber" runat="server" Text='<%# Bind("TelephoneNumber") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company" SortExpression="Company">
<ItemTemplate>
<asp:Label ID="lblCompany" runat="server" Text='<%# Eval("Company") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCompany" runat="server" Text='<%# Bind("Company") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbEditLink" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbCancelLink" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
<asp:LinkButton ID="lbUpdateLink" runat="server" CausesValidation="False" CommandName="Update" Text="Update"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void gridSearchResults_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridSearchResults.Rows[e.RowIndex];
foreach (gridviewrow c in gridSearchResults.Rows[e.RowIndex])
{
}
}
So does anyone know a good way of looping through the cells of a gridview row and grabbing the textbox value in the gridview row cell. Thanks
Try this:
foreach (TableCell cell in gv.Rows(e.RowIndex).Cells)
{
TextBox txt1 = (TextBox)cell.FindControl("txtControlName");
// or if you don't want to hard code the control name and assuming that there is only 1 control in the cell then use this:
TextBox txt2 = (TextBox)cell.Controls(0);
}