I am trying to select a row from a gridview by using a select button link but when I click on the button, it doesn't call that C# method. So, I am wondering what could I be doing wrong. Please help me out. Thanks
form.aspx-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added." Height="72px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="723px">
<Columns>
<asp:TemplateField HeaderText="Title" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lbltitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txttitle" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subtitle" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblsubtitle" runat="server" Text='<%# Eval("Subtitle") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtsubtitle" runat="server" Text='<%# Eval("Subtitle") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Content" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblContent" runat="server" Text='<%# Eval("Content") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContent" runat="server" Text='<%# Eval("Content") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="SELECT" CommandName="MyCustomCommand" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
NewsFeedDemo.cs
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("MyCustomCommand"))
{
GridViewRow clickedRow = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
Label lblID = (Label)clickedRow.FindControl("lblID");
}
}
please use this OnRowCommand="GridView1_RowCommand"
<asp:LinkButton ID="lnkbedit" runat="server" CommandName="MyCustomCommand" CommandArgument='<%#Eval("id") %>'>Edit</asp:LinkButton>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
}
}
You use the Method GridView1_RowCommand, but it is not bound to GridView1. You need to add OnRowCommand to the GridView.
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
Related
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;
}
}
}
I'm trying to add a data-th in a gridView but I don't know how, this attribute is added in a td
Something like this
<tr>
<td data-th="Movie Title">Star Wars</td>
<td data-th="Genre">Adventure, Sci-fi</td>
<td data-th="Year">1977</td>
<td data-th="Gross">$460,935,665</td></tr>
This is my asp.net code
I tried add data-th in the item template and the Templatefield but it isn't working
<asp:GridView ID="CerrTick" AllowPaging="true" PageSize="15" Width="100%" class="rwd-table" OnPageIndexChanging="CerrTick_PageIndexChanging" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="CerrTick_SelectedIndexChanged" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnC" CommandName="Select" class="button prefix bom" runat="server" Text="Seleccionar" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="N° ticket">
<ItemTemplate >
<asp:Label ID="N_Tick" runat="server" Text='<%# Eval("N° ticket") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tecnico">
<ItemTemplate>
<asp:Label ID="Tec" runat="server" Text='<%# Eval("Tecnico") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Empresa">
<ItemTemplate>
<asp:Label ID="Emp" runat="server" Text='<%# Eval("empresa") %>'></asp:Label>
<asp:Label ID="id_ticket" runat="server" Text='<%# Eval("id_ticket") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Servicio">
<ItemTemplate>
<asp:Label ID="serv" runat="server" Text='<%# Eval("servicio") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Use Code behind RowDataBound
protected void CerrTick_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCellCollection cell = e.Row.Cells;
cell[0].Attributes.Add("data-th", "Movie Title");
cell[1].Attributes.Add("data-th", "Genre");
cell[2].Attributes.Add("data-th", "Year");
cell[3].Attributes.Add("data-th", "Gross");
}
}
If you want to avoid doing it in the code behind, you could build the table using a ListView or Repeater and dynamically assign the data attribute. I'm doing something similar, although the data attribute is assigned to the label inside the gridview rather than the cell itself.
<asp:Label ID="Label1" runat="server" Text='<%#Eval("value") %>' data-frequency='<%#Eval("frequency") %>' />
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.
I am not able to display the Value in a lable in footer. Below is the code
Aspx
<asp:GridView ID="gvallAccount" runat="server" AutoGenerateColumns="False" Width="589px"
OnRowDataBound="gvallAccount_RowDataBound" OnRowCommand="gvallAccount_RowCommand">
<Columns>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="lblAccountID" runat="server" Text='<%# Bind("ProductAccountID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" Visible="True">
<ItemTemplate>
<asp:Label ID="lblProductName" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Number" Visible="True">
<ItemTemplate>
<asp:LinkButton ID="lnkAccCode" runat="server" Text='<%# Bind("ProductAccountCode") %>'
OnClick="lnkAccCode_Click" CommandName="gotoLink"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Value(KES)" Visible="True">
<ItemTemplate>
<asp:Label ID="lblBalance" runat="server" Text='<%# Bind("BalanceToDate") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C# Code
protected void gvallAccount_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "BalanceToDate"));
grdTotal = grdTotal + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotal = (Label)e.Row.FindControl("lblTotal");
lblTotal.Text = grdTotal.ToString();
}
}
catch (Exception Ex)
{
logger.Error("gvallAccount_RowDataBound : " + Ex.Message);
}
}
You need to have ShowFooter="True" attribute in the GridView tag.
<asp:GridView ID="gvallAccount" runat="server" AutoGenerateColumns="False" Width="589px"
OnRowDataBound="gvallAccount_RowDataBound" OnRowCommand="gvallAccount_RowCommand" ShowFooter="True">
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