reading values from gridview template field in asp.net - c#

Here is a code of template field in gridview . i want to get value of this field in string using c# code.
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="P"></asp:ListItem>
<asp:ListItem Value="L"></asp:ListItem>
<asp:ListItem Value="A"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
i tried the following code but its not working .
here is my code for reading value.
string str = gridview1.Rows[0].Cells[0].Text.ToString();
but its returning empty string. how can I get selected value.

You can't find the dropdown selected value like this. You need to find the control first using FindControl method:-
DropDownList DropDownList2 = (DropDownList)gridview1.Rows[0].Cells[0]
.FindControl("DropDownList2");
string str = DropDownList2.SelectedValue;

Related

Gridview event to access controls in ItemTemplate & EditItemTemplate?

Is there one gridview event that can access a control in ItemTemplate and EditItemTemplate without additional code (ie. session, viewstate, etc)?
Let's say my gridview looks like this:
<asp:GridView ID="GridView_Sales" runat="server"
AutoGenerateColumns="False"
DataKeyNames="SalesId"
OnRowDataBound="OnRowDataBound"
OnRowEditing="GridView_NSB_RowEditing"
OnRowUpdating="GridView_NSB_RowUpdating"
OnRowCommand="GridView_NSB_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView_RowDataBound has access to Label_WasSold in ItemTempplate but not the dropdown in EditItemTemplate. GridView_RowEditing has access to DropDownList_Sold but not to Label_WasSold; the same thing with GridView_RowUpdating.
I want to compare the value in Label_WasSold.Text to the value in DropDownList_Sold.SelectedValue when doing an update without having to add more code or drag session variables from one place to another.
Just add a hidden field to EditTemplate that stores the value of WasSold data item as in code below.
In your RowUpdating event, you can find the hidden field and get its value, then compare it with drop down value.
Markup to include hidden field in EditTemplate
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField id="hdnWasSold" runat="server" Value='<%# Eval("WasSold").ToString() %>' />
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
C# code to get the hidden field value in RowUpdating event
HiddenField hdnWasSold = (HiddenField)GridView_Sales.Rows[e.RowIndex].FindControl("hdnWasSold");
string wasSoldValue = hdnWasSold.Value;

Gridview must show Text instead of the Value from the database

I have a gridview that gets the data from a database.
Here is the .NET code. There's a lot of columns, and here the template: Choice is the one I'm talking about.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" CellPadding="4" DataKeyNames="EmpID" ForeColor="#333333" GridLines="None" style="margin-right: 81px" Width="1174px" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Choice" SortExpression="Choice">
<EditItemTemplate>
<asp:DropDownList ID="DropDownChoice" Text='<%# Bind("Choice") %>' runat="server" Width="60px">
<asp:ListItem Text="No" Value="0"></asp:ListItem>
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblChoice" runat="server" Text='<%# Bind("Choice") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Here on the EditItemTemplate, the user may edit the said data on the database. Where I can use a drop down list and show the text as No or Yes but the value that will be saved on the database is still 0 and 1.
<EditItemTemplate>
<asp:DropDownList ID="DropDownChoice" Text='<%# Bind("Choice") %>' runat="server" Width="60px">
<asp:ListItem Text="No" Value="0"></asp:ListItem>
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
I'm having trouble of showing the text No and Yes on the Gridview once it runs. As you can see here is the ItemTemplate code:
<ItemTemplate>
<asp:Label ID="lblChoice" runat="server" Text='<%# Bind("Choice") %>'></asp:Label>
</ItemTemplate>
Please suggest a way/method to show No/Yes on the ItemTemplate/Gridview section once executed. Thanks (let me know if you might need the C# code to bind data on the gridview)
NOTE:
I have tried the RowDataBound event that was answered in this question.
here is the code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRow dr = ((System.Data.DataRowView)e.Row.DataItem).Row;
if (dr["Choice"].ToString() == "0")
{
((Label)e.Row.FindControl("lblChoice")).Text = "No";
}
else if (dr["Choice"].ToString() == "1")
{
((Label)e.Row.FindControl("lblChoice")).Text = "Yes";
}
}
}
But it doesn't work. Am I missing something? Do I have to include something in the <ItemTemplate> tag ?
As far as I understood, you can either use Render Property function or row data bound event.
RenderProperty Function
RowDataBound Event
Both answers are from this question
Edit: As you asked how to call the RowDataBound event, it's added to the GridView tag like this:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
Without using RowBound event you can do this
<ItemTemplate>
<asp:Label ID="lblChoice" runat="server"
Text='<%# Bind("Choice").Equals("1")? "Yes" : "No" %>'></asp:Label>
</ItemTemplate>

Can't retrieve new value from GridView template field

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>

asp.net dynamically created gridview edit template field

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();
}

How to make checbox checked or uncheked in gridview editmode

i have made a gridview which has many columns and the editmode , some are text and dropdown list are are working perfect .
How can i assign value to a Checkbox to make it Checked or Unchecked?
The value against which i want to do is Yes / No.
So i am having trouble doing this.
My Gridview: Just pasting portion of it as its too big.
<asp:TemplateField HeaderText="CUpdate">
<ItemTemplate >
<asp:Label ID="Label6" runat="server" Text='<%# Eval("CUpdate") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCupdate" Text='<%# Eval("CUpdate") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate >
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Email") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList ID="checkEmail" runat="server">
<%--HOW TO MAKE THIS CHECKED OR UNCHECKED ? when i have values Yes / NO--%>
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
Update
If i make the above as :
<EditItemTemplate>
<asp:CheckBoxList ID="checkEmail" selectedvalue='<%# Eval("Email") %>' runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
In this case i do get checkbox checked when there us Yes , but in case on NO it wont run possible error as there is no value 'NO' to select in the checboxlist .Any solution.
Thank you
Your CheckBoxList needs to have all the posible values like so..
<asp:CheckBoxList ID="checkEmail" selectedvalue='<%# Eval("Email") %>' runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:CheckBoxList>
Although i don't understand why you want to use a CheckBoxList instead of just 1 checkbox like so..
<asp:CheckBox runat="server" ID="checkEmail" Checked = '<%# Eval("Email").ToString().Equals("yes", StringComparison.OrdinalIgnoreCase) %>' />

Categories