I have a GridView with Template Fields and Connection String is located in the
web.config File. Data is fetched by a stored procedure. I want to retrieve a single cell value of the Gridview in protected void someMethod(object sender, EventArgs e) on OnClick="someMethod" event. But it ends up with:
Object reference not set to an instance of an object.
Code that generates the OnClick Event:
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="someMethod">Get Name</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
The value of Gridview Cell i want to get is:
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="forNames" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="eForNames" runat="server" Text = '<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And Code Behind is:
protected void someMethod(object sender, EventArgs e)
{
Label ename = diplayAdapter.SelectedRow.FindControl("eForNames") as Label;
Label5.Text = ename.Text;
}
.ASPX Code:
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument="<%# Container.DisplayIndex %>" runat="server" CommandName="FindName">Get Name</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="forNames" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="eForNames" runat="server" Text = '<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
.CS Code
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//getting rowindex which we have selected by using CommandArgument
int rowindex = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "FindName")
{
Label5.Text= GridView1.Rows[rowindex].Cells[1].FindControl("eForNames");
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
Related
I am trying to do a thing, so, when I click on the GridView Row I want to send the CommandArgument, for now, I am doing with linkbuttons and I want to get rid of that, I want the user to click only the row and then it sends the CommanArgument.
At the moment this is my code:
<asp:GridView ID="jj" runat="server" AllowPaging="true" AllowSorting="false"
AutoGenerateColumns="false" CssClass="mGrid" GridLines="None" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" OnPageIndexChanging="gridView_PageIndexChanging" PageSize="10" EmptyDataText="Não existem dados para mostrar">
<Columns>
<asp:TemplateField HeaderText="Pré Ordens">
<ItemTemplate>
<asp:LinkButton ID="PreOrdens" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("TIPO_Registo") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Descrição do Erro">
<ItemTemplate>
<asp:LinkButton ID="DescricaoErro" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("Erro") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NUV">
<ItemTemplate>
<asp:LinkButton ID="NUV" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("NUV") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TV">
<ItemTemplate>
<asp:LinkButton ID="ErroID" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("TV") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the C#:
protected void SEND_ID_REGISTO(object sender, EventArgs e)
{
id = Convert.ToInt32(((LinkButton)sender).CommandArgument);
}
This is not the full code, is just the necessary, but yea, I need to send the command argument on the row click not on the linkbutton click.
you must use the RowCreated-Event of the GridView and You must add this on every postback . .
For example
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "select row";
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
I have a GridView, which is populated using a linq query on button click.
I have to populate the header text dynamically, fetching from the database. The first time I click the button, the header text is not binding. But, if I click on the button a second time, the header texts are bound.
Markup:
<asp:GridView ID="grdresult" runat="server" AutoGenerateColumns="false"
OnRowDataBound="grdresult_RowDataBound">
<Columns>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol1" runat="server" Text=' <%#Eval("Col1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol2" runat="server" Text='<%#Eval("Col2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("Col3") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol4" runat="server" Text=' <%#Eval("Col4") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol5" runat="server" Text='<%#Eval("Col5") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol6" runat="server" Text=' <%#Eval("Col6") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol7" runat="server" Text=' <%#Eval("Col7") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblCol8" runat="server" Text=' <%#Eval("Col8") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Records Found
</EmptyDataTemplate>
</asp:GridView>
Codebehind:
protected void btnreport_Click(object sender, EventArgs e)
{
BindGrid();
}
protected void BindGrid()
{
var results = "LINQ select query from database";
}).Distinct().ToList();
for (int i = 0; i < grdresult.Columns.Count; i++)
{
grdresult.Columns[i].Visible = true;
}
grdresult.DataSource = results;
grdresult.DataBind();
}
protected void grdresult_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
var getColumnHeader = "LINQ query to get header text";
if (getColumnHeader != null)
{
grdresult.Columns[5].HeaderText = getColumnHeader.HCol3;
grdresult.Columns[6].HeaderText = getColumnHeader.HCol4;
grdresult.Columns[7].HeaderText = getColumnHeader.HCol5;
grdresult.Columns[8].HeaderText = getColumnHeader.HCol6;
grdresult.Columns[9].HeaderText = getColumnHeader.HCol7;
grdresult.Columns[10].HeaderText = getColumnHeader.HCol8;
grdresult.Columns[11].HeaderText = getColumnHeader.HCol9;
grdresult.Columns[12].HeaderText = getColumnHeader.HCol10;
}
}
//based on some condition i have set visibility of gridview columns
int TotalColumnCount = (int)ViewState["colcount"];
for (int i = grdresult.Columns.Count - 1; i > TotalColumnCount + 4; i--)
{
grdresult.Columns[i].Visible = false;
}
}
Instead of using the below line for binding header text
grdresult.Columns[5].HeaderText = getColumnHeader.HCol3;
i used below line which solved the issue.
e.Row.Cells[5].Text = getColumnHeader.HCol3;
This error can also occur if you are dumping an integer value into varchar() but the size of varchar is not enough. For example I was getting this error as I was cast sum() to varchar(10), and hence the error.
I changed varchar(10) to varchar(20) and the error got resolved.
In Gridview control set ClientIDMode = "Static"
I've a ASP.NET GridView with the following data:
Rows will be disable OnRowDataBound based on the value on column3.
GridView :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound1">
<Columns>
<asp:TemplateField HeaderText="Column1">
<ItemTemplate>
<asp:HyperLink ID="hyperlink" runat="server" Text='<% #Eval("Dosage") %>' NavigateUrl='<% #Eval("Dosage") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<% #Eval("Drug") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column3">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<% #Eval("Patient") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column4">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<% #Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
RowDataBound :
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label a = e.Row.FindControl("Label3") as Label;
if (a.Text == "Sam")
{
e.Row.Enabled = false;
e.Row.Cells[0].Enabled = true;
}
}
}
however, I want column1 always enable, hyperlink in column1 should always clickable.
I've tried get the cells and enabled it, but it is not working.
kindly advise what is the workaround for the issue above.
You can do this by enable/disable particular cell.
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label a = e.Row.FindControl("Label3") as Label;
if (a.Text == "Sam")
{
e.Row.Cells[0].Enabled = true;
e.Row.Cells[1].Enabled = false;
e.Row.Cells[2].Enabled = false;
e.Row.Cells[3].Enabled = false;
}
}
}
Im having trouble with my update command. It finds the textbox controls but it doesn't get the new values entered into it and i can't figure out why. i've done some tutorials and im not getting far. In my Update event is a class called Pages which updates the text and that works if i add the text values manually. The problem is accessing the newly updated text from my textboxes.
<asp:GridView ID="CustomGridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Name" AutoGenerateSelectButton="true"
ShowHeaderWhenEmpty="True" ShowFooter="true" AutoGenerateEditButton="true" OnDataBound="CustomGridView_DataBound"
OnRowEditing="CustomGridView_CancelEditCommand" OnRowCommand="CustomGridView1_RowCommand"
OnLoad="CustomGridView1_Load" OnRowUpdated="CustomGridView1_RowUpdated" OnRowUpdating="CustomGridView1_RowUpdating" OnRowCancelingEdit="CustomGridView1_RowCancelingEdit"
ShowHeader="true">
<Columns>
<asp:TemplateField HeaderText="Page Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Path" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Eval("Path") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Path" runat="server" Text='<%# Bind("Path") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Route Value" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Eval("RouteValue") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RouteValue" runat="server" Text='<%# Bind("RouteValue") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RegExp" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Eval("RegExp") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExp" runat="server" Text='<%# Bind("RegExp") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This part here isn't firing i prefere the method RowUpdated Method in the answer is there a reason why my Updated Event is not firing
protected void CustomGridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception == null && e.AffectedRows == 1)
{
Pages pages = new Pages();
SystemPage SySPage = new SystemPage();
SySPage.Name = e.NewValues[0].ToString();
SySPage.Path = e.NewValues[1].ToString();
SySPage.RouteValue = e.NewValues[2].ToString();
SySPage.RegExp = e.NewValues[3].ToString();
pages.Update(SySPage, xmlFile);
CustomGridView1.EditIndex = -1;
BindData();
}
}
protected void CustomGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
for (int i = 0; i < CustomGridView1.Columns.Count - 1; i++)
{
DataControlFieldCell cell = CustomGridView1.Rows[CustomGridView1.EditIndex].Cells[i] as DataControlFieldCell;
CustomGridView1.Columns[i].ExtractValuesFromCell(e.Keys, cell, DataControlRowState.Edit, false);
}
Pages pages = new Pages();
SystemPage SysPage = new SystemPage();
SysPage.Name = e.NewValues[0].ToString();
SysPage.Path = e.NewValues[1].ToString();
SysPage.RouteValue = e.NewValues[2].ToString();
SysPage.RegExp = e.NewValues[3].ToString();
pages.Update(SysPage, xmlFile);
lblInsert.Text = e.NewValues[3].ToString();
CustomGridView1.EditIndex = -1;
BindData();
}
To get the values AFTER updating you should handle the GridViews RowUpdated Event http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdated.aspx not the RowUpdating.
<asp:GridView OnRowUpdated="GridViewUpdatedEventHandler" />
Edit
The updated fields in the GridView are exposed by a property, NewValues, in the GridViewUpdatedEventArgs which is exposed as a parameter of the RowUpdated event.
Code as requested I cannot test this code as I am not near a dev pc.
Add the following method to your code file and set the GridViews OnRowUpdated property to point to the method.
protected void CustomGridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if ((e.Exception == null) && (e.AffectedRows == 1))
{
Pages pages = new Pages();
SystemPage SysPage = new SystemPage();
SysPage.Name = e.NewValues[0].ToString();
SysPage.Path = e.NewValues[1].ToString();
SysPage.RouteValue = e.NewValues[2].ToString(); ;
SysPage.RegExp = e.NewValues[3].ToString(); ;
pages.Update(SysPage, xmlFile);
CustomGridView1.EditIndex = -1;
BindData();
}
else
// TO DO: ALERT the user the update errored
}
hai pupils
I am doing a project on online education. In my project i display the trainees/trainers in in a gridview. If i select any data from the gridview it has to be dispalyed in the label kept below. How can i do this?
Thanks in advance for those who answers.
Use asp:LinkButton to show data in each column. Set commandArgument as the data. set same comandname and handle rowcommand in gridview rowcommand event.
try the following:
in aspx page:
<asp:GridView ID="GridView1" OnRowCommand="GridView1_RowCommand" runat="server">
<Columns>
<asp:TemplateField HeaderText="CategoryID">
<ItemTemplate>
<asp:LinkButton ID="lnkID" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName">
<ItemTemplate>
<asp:LinkButton ID="lnkName" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblCat" runat="server"></asp:Label>
in aspx.cs page:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "sel")
{
lblCat.Text = e.CommandArgument;
}
}