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..
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 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>
I need to use DataList inside another DataList. It works fine for me, but when I'm trying to do something in code behind with this inside one it just doesn't exist for C#. Here is code:
...
<asp:DataList ID="DataListDziennik" runat="server"
DataSourceID="SqlDataSourcePrzedmioty">
<ItemTemplate>
<asp:Label ID="LabelPrzedmiot" runat="server" Text='<%# Eval("przedmiot") %>' />
...
<asp:DataList ID="DataListOceny" runat="server"
DataSourceID="SqlDataSourceOceny"
RepeatDirection="Horizontal"
OnItemCommand="DataListOceny_ItemCommandOceny"
OnEditCommand="DataListOceny_EditCommandOceny">
<EditItemTemplate>
<asp:TextBox ID="TextBoxOcena" runat="server" Text='<%# Bind("lista") %>' />
<td><asp:Button ID="ButtonZapisz" CommandName="Update" runat="server" Text="Zapisz" /></td>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox Width="20" ID="TextBoxOcena" ReadOnly="true" Text='<%# Eval("lista") %>' runat="server"></asp:TextBox>
<td><asp:Button ID="ButtonEdytuj" CommandName="Edit" runat="server" Text="Edytuj" /></td>
</ItemTemplate>
</asp:DataList>
</td>
</ItemTemplate>
</asp:DataList>
When I write this in code behind:
protected void DataListOceny_EditCommand(object source, DataListCommandEventArgs e)
{
DataListOceny.EditItemIndex = e.Item.ItemIndex;
DataListOceny.DataBind();
}
...Visual Studio tells me that DataListOceny does not exist in current content. I just want to be able edit items on DataListOceny after clicking the "edit" button, it can be placed anywhere on website. Do you know any solution for this problem?
Because DataListOceny is a control inside of another control, you have to make a reference to it by doing something like:
DataList DataListOceny = (DataList)e.Item.FindControl("DataListOceny");
Once you do that, you can use the DataListOceny variable. Hope this helps.
I have a grid view in my page in which i added a template feild.inserted a button which upon clicking will redirect the user to the actual request to make edits. The button that i added in the item template is bound to the data.The below is the code for the template feild
<asp:TemplateField HeaderText="RequestId" SortExpression="roc_id">
<EditItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("roc_id") %>'></asp:HyperLink>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text='<%# Bind("roc_id") %>' />
</ItemTemplate>
</asp:TemplateField>
how do i read the text from the botton click and redirect the user to the request that they click.I have listed the url below and i need to read the requestid at the end of url
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("http://xxxxxxxxxxxxxxxxxxxxxxxxxxx/Edit.asp?ROCID=
}
Thanks
You can use GridView.RowCommand to capture the button click event.
Also, you need to add a command name to the button in template field.
There is a good article about Respond to Button Events in a GridView Control which will help you
I have to add a textbox inside a form that is composed of a lot of textboxes, and one button at the end. It has a datasource, all data is loaded in formload, and the button updates the values of the textboxes. The thing is: this particular textbox won't be in the datasource, I want to get this from the web.config, and I've already managed to change the web.config in other page, but in this case, the textbox ID won't appear in intellisense in the code-behind of the page itself, so I figured it's unaccessible to anything besides the pure binding of the form.
<asp:FormView>
<EditItemTemplate>
<asp:TextBox ID="id" runat="server" Text='<%# bind("field") %>'/>
<asp:TextBox ID="id2" runat="server" Text='<%# bind("field2") %>'/>
<asp:TextBox ID="id3" runat="server" Text='<%# bind("field3") %>'/>
<asp:TextBox ID="THIS_ONE" runat="server"></asp:TextBox> <!--HERE-->
<asp:Button ID="UpdateButton" runat="server" SkinID="UpdateButton" CommandName="Update"/>
</EditItemTemplate>
</asp:FormView>
Above, I have an unaccessible textbox.
<asp:FormView>
<EditItemTemplate>
<asp:TextBox ID="id" runat="server" Text='<%# bind("field") %>'/>
<asp:TextBox ID="id2" runat="server" Text='<%# bind("field2") %>'/>
<asp:TextBox ID="id3" runat="server" Text='<%# bind("field3") %>'/>
<asp:Button ID="UpdateButton" runat="server" SkinID="UpdateButton" CommandName="Update"/>
</EditItemTemplate>
</asp:FormView>
<asp:TextBox ID="THIS_ONE" runat="server"></asp:TextBox><!--HERE-->
Above, I have an accessible textbox, but BELOW the update button.
I've already tried closing EditItemTemplate before the textbox and re-opening it afterwards. Doesn't work.
I could of course put it below the button, below where the form ends, then they wouldn't be part of the form, and that would work, but what if I want the textboxes ABOVE the button ? I want accessible unbinded textboxes inside an ASP.NET formview. Is that possible ?
ps.: I know the implications of messing with web.config in run-time and I know that this doesn't seem well planned, but I didn't say some details that don't matter for this question.
If your issue trying to access the textbox? You can use FormView1.FindControl() to obtain a reference to the control within the formview...