I am having trouble retrieving the new value that is entered in a textbox template field in my GridView.
Here is my markup:
<asp:TemplateField HeaderText="username" SortExpression="username">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
And here is how I'm trying to retrieve the new value, inside the RowCommand event handler of my GridView:
string userName = ((TextBox)grdUserList.Rows[rowIndex].FindControl("txtUserName")).Text;
I get the old value instead of the newly typed value when I execute this code.
Does anyone know what I am missing? Thanks in advance.
I just found out the solution for my problem. I searched and found out that the GridView is being refreshed before the retrieving process begins because I was rebinding the GridView on the Page_Load method. I fixed the problem by not rebinding the gridview when it is a post back (or at least not before I have made the changes) using the IsPostback method. Thanks for everyone's reply :)
You are retrieving new value in wrong GridView event. You have to add OnRowUpdating="grdUserList_RowUpdating" event in your GridView control and then retrieve new TextBox value.
OnRowUpdating event in code-behind:
protected void grdUserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userName = ((TextBox)grdUserList.Rows[e.RowIndex].FindControl("txtUserName")).Text;
// Write your update query and logic over here.
}
You can take a reference from here for additional knowledge.
Please let me know if you have any questions.
use this code in gridview
<Columns>
<asp:TemplateField HeaderText="SrNo">
<EditItemTemplate>
<asp:TextBox ID="txtsrno" runat="server" Text='<%#Eval("SrNo") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblsrno" runat="server" Text='<%#Eval("SrNo") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Related
I have following field in a grid view:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("JobEstimation").ToString() == "1" ? "Yes" : "No" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now how do I enable sorting in this filed? "SortExpression="???"".
So when the user click the sort it should sort base on alphabet order.
As stated in comments, it would be better to implement that conversion within the Database, but pertinent to you question, the answer would depend on the actual content of that field. As a simple solution, you may try the following code snippet:
<asp:TemplateField HeaderText="Job Est" SortExpression="JobEstimation">
<ItemTemplate>....</ItemTemplate>
</asp:TemplateField>
Alternatively, you may add the Command/Link Button to the HeaderTemplate:
<asp:TemplateField SortExpression="JobEstimation">
<HeaderTemplate>
<asp:LinkButton ID="sortJobEst" runat="server" Text="Job Est" CommandName="Sort" CommandArgument="JobEst" />
</HeaderTemplate>
</asp:TemplateField>
and hook it to the GridView RowCommand event (re: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.110).aspx):
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Sort"))
{
// add your code to sort
e.CommandArgument.ToString()...
BindGridView();
}
}
Hope this may help.
I have a gridview to which I dynamically bind data to.
It features SELECT and EDIT commands.
When it is in EDIT mode all the fields turn into textboxes that can be edited.
What I would like to do is add an Ajax Calendar Extender to one of the textboxes.
How can I create a TemplateField just for the one field?
Is it even possible? I know how to do it if I bound all fields and assign the data via a SqlDataSource.
I have tried adding the templatefield to my gridview but the data is just not showing up:
<asp:GridView ID="gvCheckResults" runat="server" OnRowDataBound="gvCheckResults_RowDataBound"
OnRowEditing="gvCheckResults_RowEditing" OnRowUpdating="gvCheckResults_RowUpdating" OnRowCancelingEdit="gvCheckResults_RowCancelingEdit"
OnRowDeleting="gvCheckResults_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="DateOfTest" SortExpression="DateOfTest">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="test" runat="server"
TargetControlID="TextBox1"
CssClass="calendar"
Format="dd/MM/yyyy">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How the data gets bound:
protected void TestDataBind()
{
string Name = txtCheckName.Text;
if (string.IsNullOrEmpty(Name))
Name = null;
gvCheckResults.DataSource = dataContext.GetRecords(Name);
gvCheckResults.DataBind();
}
I have a GridView which is pretty functioning except for one thing: when I click the Edit link button, it appears a list of textboxes to edit the values as I want. But, when I click the update button and do ((TextBox)row.Cells[2].Controls[0]).Text (for example), it returns me the value that was there before the I edited the textbox!
Any idea of how to access the new texts of the textboxes?
Hey You have to do on Gridview_RowUpdating command like that
protected void gvSalaryDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
((TextBox)gvSalaryDetails.Rows[e.RowIndex].FindControl("txtgvSalaryHeadValue")).Text;
}
and Aspx page shoul follow like that.
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<div class="control">
<asp:Label ID="lblgvSalaryHeadValue" runat="server" Text='<%# ((SalaryDetails)Container.DataItem).HeadValue %>'></asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtgvSalaryHeadValue" runat="server" Text='<%# ((SalaryDetails)Container.DataItem).HeadValue %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Hope it Helps you..
I want to get the value of a hidden field in a grid view from code-behind, but not to be used in the _RowDataBound or any other similar method. Here is my present code (it is a shopping cart scenario):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID label in the _TextChanged event. In this same event, I tried
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
but it didn't work and returns only a null value. What is the solution?
For each row in your GridView there is a HiddenField for the ProductID.
You can access the HiddenField of a row (in the example below the first row) by using the following code (assuming your HiddenField is in the first cell):
HiddenField hiddenFieldProductID =
(HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");
string productID = hiddenFieldProductID.Value
// Do something with the value
Hope, this helps.
Try to replace the HiddenField to a label or a textbox and set the visible attribute to false.
I had tried this before and it works.
i think this maybe an easy one...
I have a Gridview, bound to a dataSource with this query:
select userid, name, phone from users
I need to change the content of the Name cell to a link, like so:
User's Name
so OnRowDataBound i'm doing this:
e.Row.Cells[1].Text = "" + e.Row.Cells[1].Text + "";
It all works fine this way. My problem is that i don't want to display the UserId column in the Gridview, but when i attribute it (asp attribute or server-side) Visible="false" to the BoundField UserId, i can't capture it's value to build the Name Cell.
Thanx for your help
Make your gridView to use template items instead of simple grivdiew column.
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName")%>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
You can find a nice intro Here
Agree with Jani, this should be done with a template field. If you really need to do it the other way, see Can data be kept in a dynamically bound GridView's invisible fields?.
you can set the visibility on the onrowdatabound event