Gridview eval condition is not working properly - c#

I am having database table with subject,faulty,hour.In the hour value am storing like 1,2,3,4 and 5 in the database.I want to disable the already clicked check box but in my result it is blocking the whole row not the particular check box.help me out from these problem.I have uploaded my sample code and database schema below
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="datee" HeaderText="Day/Hour" SortExpression="datee" />
<asp:TemplateField HeaderText="Hour1">
<EditItemTemplate>
<asp:CheckBox ID="chkColumn1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour1" runat="server" Checked='<%# Convert.ToBoolean((int)Eval("hour"))%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=1) %>' OnCheckedChanged="chkHour1_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour2">
<EditItemTemplate>
<asp:CheckBox ID="ChkColumn2" runat="server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour2" runat="server" Checked='<%#Convert.ToBoolean((int)Eval("hour"))%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=2) %>' OnCheckedChanged="chkHour2_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour3">
<EditItemTemplate>
<asp:CheckBox ID="chkColumn3" runat="server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour3" runat="server" Checked='<%#Convert.ToBoolean((int)Eval("hour"))%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=3) %>' OnCheckedChanged="chkHour3_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour4">
<EditItemTemplate>
<asp:CheckBox ID="chkColumn4" runat="server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour4" runat="server" Checked='<%#Convert.ToBoolean((int)Eval("hour"))%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=4) %>' OnCheckedChanged="chkHour4_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour5">
<EditItemTemplate>
<asp:CheckBox ID="chkColumn5" runat="server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkHour5" runat="server" Checked='<%#Convert.ToBoolean((int)Eval("hour"))%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=5) %>' OnCheckedChanged="chkHour5_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
con.Open();
SqlDataAdapter cmd = new SqlDataAdapter("select DISTINCT datee,hour from tblfac order by datee", con);
DataTable dt = new DataTable("dt");
cmd.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
I clicked only 1 check box value but it is blocking the whole column.help me out from the problem

If I'm understanding the question correctly, the problem is that all five checkboxes in the row are checked, regardless of what the value of hour is. This is because you are using
Checked='<%#Convert.ToBoolean((int)Eval("hour"))%>'
to set the value of the checkbox. You are converting the integer value of hour to a boolean value. Convert.ToBoolean will translate any non-zero integer to true, so as long as hour isn't zero, the checkbox will be checked.
Working example:
Creating datasource in code-behind:
DataTable dt = new DataTable("dt");
dt.Columns.Add("datee", typeof(string));
dt.Columns.Add("hour", typeof(int));
dt.Rows.Add("06-Jul-15 12:00:00 AM",1);
dt.Rows.Add("07-Jul-15 12:00:00 AM",2);
dt.Rows.Add("08-Jul-15 12:00:00 AM",3);
dt.Rows.Add("09-Jul-15 12:00:00 AM",4);
dt.Rows.Add("10-Jul-15 12:00:00 AM",5);
GridView1.DataSource = dt;
GridView1.DataBind();
Gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="datee" HeaderText="Day/Hour" SortExpression="datee" />
<asp:TemplateField HeaderText="Hour1">
<ItemTemplate>
<asp:CheckBox ID="chkHour1" runat="server" Checked='<%# Convert.ToBoolean((int)Eval("hour")==1)%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=1) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour2">
<ItemTemplate>
<asp:CheckBox ID="chkHour2" runat="server" Checked='<%#Convert.ToBoolean((int)Eval("hour")==2)%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=2) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour3">
<ItemTemplate>
<asp:CheckBox ID="chkHour3" runat="server" Checked='<%#Convert.ToBoolean((int)Eval("hour")==3)%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=3) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour4">
<ItemTemplate>
<asp:CheckBox ID="chkHour4" runat="server" Checked='<%#Convert.ToBoolean((int)Eval("hour")==4)%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=4) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hour5">
<ItemTemplate>
<asp:CheckBox ID="chkHour5" runat="server" Checked='<%#Convert.ToBoolean((int)Eval("hour")==5)%>' Enabled='<%# Convert.ToBoolean((int)Eval("hour")!=5) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Results showing only one box checked, and that box disabled:

Related

How to obtain and display specific value in Gridview on ASP.NET C#

I am currently developing an online shopping cart project.
I would like the program to display the GetTotal() in a label outside of the GridView.
<asp:Content ID="Content1" ContentPlaceHolderID="C1" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Width="832px" >
<Columns>
<asp:TemplateField HeaderText="Product Image">
<ItemTemplate>
<asp:Image ImageUrl='<%# "../" + Eval("Product_Image") %>' runat="server" Height="100px" Width="100px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Series">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Product_Series") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("Product_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("Product_Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Quantity">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
Total Amount:
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price">
<ItemTemplate>
<%# GetUnitPrice(Decimal.Parse(Eval("Product_Price").ToString())*Decimal.Parse(Eval("Product_Quantity").ToString())).ToString("N2") %>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="l6" runat="server" Text=' <%# GetTotal().ToString("N2") %>'></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true"/>
<asp:CommandField ShowDeleteButton="true"/>
</Columns>
</asp:GridView>
All values in the GridView are passed on via session from another page, excluding Price and TotalUnitPrice, where calculations are carried out in the script below:
<script runat="server">
decimal TotalUnitPrice;
decimal GetUnitPrice(decimal Price)
{
TotalUnitPrice += Price;
return Price;
}
decimal GetTotal()
{
return TotalUnitPrice;
}
</script>
Any ideas on how to do this? The reason is that I would like the program to obtain the value of GetTotal()via the label and charge the user based on it.
Your GetTotal() in binding in GridView footer Label 16 so you can get it from there and set to outside Label:
// check if GridView is not empty
if (GridView1.Rows.Count != 0)
{
string total = ((Label)GridView1.FooterRow.FindControl("16")).Text;
LabelOutside.Text = total;
}

Get gridview row values when checkbox checked

I have a gridview with checkboxes to select the row. On checking the checkbox I need to get the row values into a string/session. Below is the code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_OnRowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width ="1000px" class="grid" AllowPaging="True" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" PageButtonCount="2" PagerSettings-Mode="NumericFirstLast" PageSize="5">
<PagerSettings Mode="NumericFirstLast" PageButtonCount="2" FirstPageText="First" LastPageText="Last"/>
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:CheckBox ID="CheckBox3" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Connection">
<ItemTemplate>
<asp:Label ID="lbl_conn" runat="server" Text='<%#Eval("Connection") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserID">
<ItemTemplate>
<asp:Label ID="lbl_Usrid" runat="server" Text='<%#Eval("UserID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Usrid" runat="server" Text='<%#Eval("UserID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lbl_pwd" runat="server" Text='<%#Eval("Password") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_pwd" runat="server" Text='<%#Eval("Password") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Connection Name">
<ItemTemplate>
<asp:Label ID="lbl_conName" runat="server" Text='<%#Eval("Connection_Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_conName" runat="server" Text='<%#Eval("Connection_Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" Text=" Edit" class=" btnEdit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" class=" btnEdit" Text="Update" CommandName="Update"/>
<asp:Button ID="btn_Cancel" runat="server" class=" btnEdit" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="btn_Delete" runat="server" class=" btnDelete" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
There is a button below the grid. on click of the button I need to get the values.
protected void LinkButton1_Click(object sender, EventArgs e)
{
foreach (GridViewRow item in GdvTestData.Rows)
{
CheckBox chk = (item.FindControl("CheckBox3") as CheckBox);
if (chk.Checked)
{
string conn = item.Cells[1].Text;
}
}
}
But am getting null value for string conn = item.Cells[1].Text;
where am I going wrong
Grid contains the different row type like header row , data row and footer row. You need to get content from the data row only then please check the row type first if it is a data row then try to get cell values. GridViewRow.RowType Property
foreach(GridViewRow item in GdvTestData.Rows) {
// check row is datarow
if (item.RowType == DataControlRowType.DataRow) {
CheckBox chk = (item.FindControl("CheckBox3") as CheckBox);
if (chk.Checked)
{
Label MyLabel = (Label)item.FindControl("lbl_conn");
string conn = MyLabel.Text;
}
}
}

Select Command and Search command are not working properly in gridview

all my commands are working properly but the problem is
after I execute my Search command then I click the select link button on my GridView, My Gridview is refreshing and select the first row in the grid view and not the one that I searched.. example: first I run the Index Page then the GridView get all the record from database(10 populated record) and show to the index.. (No Problem)
then I type the firstname of the user to search it then i click the search button then the it filtered and show to gridview again here's the image
then I click the "Select" link button from Gridview and this what happened
that's what happened .. I clicked the select link button on "marcy's record" then it refreshed and get the value of RASI's Record on the first row...
but when my click select without searching it worked just fine
here's my code for search button
string strconn;
strconn = "select * from User_TBL_DB where (Firstname like '%'+#search+'%' and Middlename like '%'+#search2+'%' and Lastname like '%'+#search3+'%')";
SqlCommand xp = new SqlCommand(strconn, conn);
xp.Parameters.Add("#search", SqlDbType.NVarChar).Value = firstname_search.Text;
xp.Parameters.Add("#search2", SqlDbType.NVarChar).Value = middlename_search.Text;
xp.Parameters.Add("#search3", SqlDbType.NVarChar).Value = lastname_search.Text;
conn.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
and here's my code for select link button on gridview
row = GridView1.SelectedRow;
firstname_tb.Text = (row.FindControl("lbl_Firstname") as Label).Text;
middlename_tb.Text = (row.FindControl("lbl_Middlename") as Label).Text;
lastname_tb.Text = (row.FindControl("lbl_Lastname") as Label).Text;
age_tb.Text = (row.FindControl("lbl_Age") as Label).Text;
id_tb.Text = (row.FindControl("lbl_ID") as Label).Text;
string gender = (row.FindControl("lbl_Sex") as Label).Text;
if (gender == "FEMALE")
{
female_rb.Checked = true;
male_rb.Checked = false;
}
else
{
male_rb.Checked = true;
female_rb.Checked = false;
}
and here's my codes for ASP on Gridview
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="581px"
onrowcommand="GridView1_RowCommand"
onselectedindexchanging="GridView1_SelectedIndexChanging">
<Columns>
<%--<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="Select_button" CommandArgument ='<%# Eval("ID") %>' CommandName="SelectRow" ForeColor="#8C4510" runat="server">Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:CommandField ShowSelectButton="True"></asp:CommandField>
<asp:TemplateField HeaderText="Firstname" SortExpression="Firstname">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Firstname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Firstname" runat="server" Text='<%# Bind("Firstname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middlename" SortExpression="Middlename">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Middlename") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Middlename" runat="server" Text='<%# Bind("Middlename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lastname" SortExpression="Lastname">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Lastname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Lastname" runat="server" Text='<%# Bind("Lastname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age" SortExpression="Age">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Age" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sex" SortExpression="Sex">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Sex") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_Sex" runat="server" Text='<%# Bind("Sex") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True"
ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399"
HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True"
ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
i connect it to sqlDatasource1 and disconnect it and leave the code as is. I used Showselectbutton="true"
that's my Problem hope you can help me thank you very much

Find control in gridview return null

I have a gridview written in asp.net (c#), the problem is when try to read data from the textbox return null.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Week#/Day"
OnRowDataBound="GridView1_RowDataBound"
onrowediting="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="false"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" AllowPaging="true"
PageSize="4" >
<Columns>
<asp:ButtonField ButtonType="Link" CommandName="Update" text="Update" />
<asp:ButtonField ButtonType="Link" CommandName="Edit" text="Edit"/
<asp:TemplateField HeaderText="Week#/Day" InsertVisible="False"
SortExpression="Week#/Day">
<EditItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#
Eval("Week#/Day") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saturday" SortExpression="Saturday">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Saturday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%# Bind("Saturday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sunday" SortExpression="Sunday">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Sunday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# Bind("Sunday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Monday" SortExpression="Monday">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"
Text='<%# Bind("Monday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server"
Text='<%# Bind("Monday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tuesday" SortExpression="Tuesday">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"
Text='<%# Bind("Tuesday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server"
Text='<%# Bind("Tuesday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Wednesday"
SortExpression="Wednesday">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("Wednesday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server"
Text='<%# Bind("Wednesday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am fill it using datatable on edit call this method
protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
and on update call update method
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session["All_Topics"];
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox d1, d2, d3, d4, d5;
d1 = (TextBox)row.FindControl("TextBox1");
d2 = (TextBox)row.FindControl("TextBox2");
d3 = (TextBox)row.FindControl("TextBox3");
d4 = (TextBox)row.FindControl("TextBox4");
d5 = (TextBox)row.FindControl("TextBox5");
dt.Rows[row.DataItemIndex]["Saturday"] = d1.Text;
dt.Rows[row.DataItemIndex]["Sunday"] = d2.Text;
dt.Rows[row.DataItemIndex]["Monday"] = d3.Text;
dt.Rows[row.DataItemIndex]["Tuesday"] = d4.Text;
dt.Rows[row.DataItemIndex]["Wednesday"] = d5.Text;
}
the problem is when read data from the textbox it is always null. How can I solve this problem.
Hey please Make sure that do not re-binding the GridView on the PostBack of the page. This may be the cause of problem.
Always bind your grid on page load in side the below code.
if (!Page.IsPostBack ){
// Code to bind the Grid
// BindData();
}
when you are click on Row Update Command it is firstly go to the page load event and on page load if you are not binding your grid like the given manner your grid is rebind and you will get null from text box.
Hope it will helps you.

Updating GridView Row from Code Behind

When the user changes the text in the textbox's in the edit template and clicks update, when I try to grab those new values it still is graving the old value of the text box.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CompanyID" CellPadding="4"
GridLines="None" Width="1079px" ForeColor="#333333"
OnRowCancelingEdit="GridView1_RowCancelling"
OnRowUpdating="GridView1_RowUpdating"
OnRowEditing="GridView1_RowEditing">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" CommandArgument='<%# Eval("CompanyID") %>' Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Issue Date">
<ItemTemplate>
<asp:Label runat="server" ID="IssueDate" Text='<%#Eval("IssueDate") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtIssueDate" Text='<%#Eval("IssueDate") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notice Intent Response Due">
<ItemTemplate>
<asp:Label runat="server" ID="NoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtNoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deadline For Questions">
<ItemTemplate>
<asp:Label runat="server" ID="DeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtDeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bids Due">
<ItemTemplate>
<asp:Label runat="server" ID="BidsDue" Text='<%#Eval("BidsDue") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtBidsDue" Text='<%#Eval("BidsDue") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shortlist Notice">
<ItemTemplate>
<asp:Label runat="server" ID="ShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Final Selection">
<ItemTemplate>
<asp:Label runat="server" ID="FinalSelection" Text='<%#Eval("FinalSelection") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtFinalSelection" Text='<%#Eval("FinalSelection") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false" HeaderText="CompanyID">
<ItemTemplate>
<asp:Label runat="server" ID="CompanyID" Text='<%#Eval("CompanyID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The update button calls this function:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");
TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));
TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");
Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
Response.End();
TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");
TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");
TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");
TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
}
The response is showing me that the value being grabbed is still the origonal text value of the box. Not what you typed into the box.
In your grid view row updating event add the following condition
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit )
{
int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");
TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));
TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");
Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
Response.End();
TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");
TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");
TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");
TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
}
}
Update:
the problem looks like that you have also bind your Edit Item template columns with the data from data table, and when you are getting the data in the code behind you are not getting the updated data which the user updates in edit mode and u still getting the old data. If you remove the Binding from the Edit Item Template feilds then your code will work.
I figured it out, Derek was right. It had to do with the Binding Data on postback in page load. I binded the data every time instead of just the first time. Thanks

Categories

Resources