Gridview value to a parameter - c#

I have a Home.aspx and Home.aspx.cs
I have a gridview in my Home.aspx >>>
<asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="False" ShowFooter="True"
CellPadding="4" ForeColor="#333333" GridLines="None" Height="281px" style="margin-top: 0px" Width="100%"
OnRowCancelingEdit="DataGridView_RowCancelingEdit"
OnRowEditing="DataGridView_RowEditing" OnRowUpdating="DataGridView_RowUpdating" HorizontalAlign="Center"
onrowdatabound="DataGridView_RowDataBound">
<AlternatingRowStyle BackColor="Lavender" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>Data 1</HeaderTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate><asp:Label ID="description" runat="server" Text='<%# Bind("description")%>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Editdescription" runat="server">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>SINGLE</asp:ListItem>
<asp:ListItem>DOUBLE</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<%-- <FooterTemplate>
</FooterTemplate>--%>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Data 2</HeaderTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate><asp:Label ID="pkgcode" runat="server" Text='<%# Bind("pkgcode") %>'></asp:Label></ItemTemplate>
<EditItemTemplate><asp:TextBox ID="Editpkgcode" runat="server" Text='<%# Bind("pkgcode") %>'></asp:TextBox></EditItemTemplate>
<%--<FooterTemplate><asp:TextBox ID="pkgcode" runat="server"></asp:TextBox></FooterTemplate>--%>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Data 3</HeaderTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate><asp:Label ID="oprcode" runat="server" Text='<%# Bind("oprcode") %>'></asp:Label></ItemTemplate>
<EditItemTemplate><asp:TextBox ID="Editoprcode" runat="server" Text='<%# Bind("oprcode") %>' ></asp:TextBox></EditItemTemplate>
<%--<FooterTemplate><asp:TextBox ID="oprcode" runat="server"></asp:TextBox></FooterTemplate>--%>
</asp:TemplateField>
</Columns>
</asp:GridView>
In my Home.aspx.cs, I have this >>
protected void DataGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ }
This is where I plan to update my data from my gridview, but before updating I want to pass the old data to a parameter so user can compare/see the changes he made. For checking purposes so I can see if it really get the data, I put the following inside DataGridView_RowUpdating (and I don't know if it's wrong)>>
GridViewRow row = DataGridView.Rows[e.RowIndex];
string #editpkgcode = (row.FindControl("pkgcode") as Label).Text;
Literal1.Text = "TEST: " + #editpkgcode;
It gives me the error : NullReferenceException was unhandled by user code

The reason why you get NullReferenceException is because when RowUpdating event fires then the EditItemTemplate exists but not ItemTemplate.
Since you defined pkgcode Label in ItemTemplate, therefore this label control is not existing when RowUpdating event fires. But, because the EditItemTemplate exists when this event fires, so you can access the Editpkgcode textbox defined in EditItemTemplate.
Therefore, you should be using the following code in your RowUpdating event in order access the textbox in EditItemTemplate.
GridViewRow row = DataGridView.Rows[e.RowIndex];
string #editpkgcode = (row.FindControl("Editpkgcode") as TextBox).Text;

Related

"Unable to cast object of type 'PanelNamingContainer' to type 'Telerik.Web.UI.GridItem"

I am in the process of converting a grid to a batch editing grid template for an ASP.NET application. On a SelectedIndexChanged event I am trying to store these controls using the FindControl() method but I am get an error
Unable to cast object of type 'PanelNamingContainer' to type 'Telerik.Web.UI.GridItem'
on the 2nd line. I tried converting to just a control. I no longer get the error but then tbIA and rcbGrades end up being NULL. What can I do?
Server Side Code
protected void RadCB1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
in_c_Merit_Promotion_Flag = e.Value;
GridDataItem item = (GridDataItem)((RadComboBox)sender).NamingContainer;
RadNumericTextBox tbIA = (RadNumericTextBox)item.FindControl("tbIA");
RadComboBox rcbGrades = (RadComboBox)item.FindControl("rcbGrades");
Client side code
<telerik:GridTemplateColumn HeaderStyle-BackColor="#00505C" HeaderStyle-ForeColor="White" DataField="Type_Of_Increase" HeaderText="Type Of Salary Increase" AllowFiltering="false"
EditFormColumnIndex="5" UniqueName="Type_Of_Increase" Visible="True" ReadOnly="False">
<ItemTemplate>
<asp:Label ForeColor="Black" BorderColor="LightSlateGray" runat="server" ID="lblTypeOfIncrease" Text='<%# Bind("Type_Of_Increase") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox runat="server" MarkFirstMatch="true" OnSelectedIndexChanged="RadCB1_SelectedIndexChanged" Skin="Web20" Width="50px"
AutoPostBack="true" ID="cboIncreaseType" datavaluefield="Text" datatextfield="Text" datasourceid="ObjDsSalIncType">
</telerik:RadComboBox>
<telerik:RadComboBox runat="server" Skin="Web20" Width="90px" MarkFirstMatch="true" AutoPostBack="false" ID="rcbGrades" Visible="false">
</telerik:RadComboBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" BackColor="LightSlateGray" Width="100px" ></HeaderStyle>
<ItemStyle BorderColor="LightSlateGray" BackColor="LightYellow" ForeColor="Black" Wrap="false" />
</telerik:GridTemplateColumn>`enter code here

i have 3 textboxes in gridview. When value is entered to first and second textboxes i want automatically write result to third textbox

i have 3 textboxes in gridview. When value is entered to first and second textboxes i want automatically write result to third textbox.
this is my gridview:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="892px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Ales Puanı">
<ItemTemplate>
<asp:TextBox ID="Txt_Ales" runat="server" Text='<%#Eval("alesSinavPuani") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dil Puanı">
<ItemTemplate>
<asp:TextBox ID="txt_Dil" runat="server" Text='<%#Eval("dilSinavPuani") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Toplam">
<ItemTemplate>
<asp:Label ID="lbl_Toplam" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
1) Mention AutoPostBack="true" for 2 textbox's
2) Mention event OnTextChanged="Txt_TextChanged" for 2 textbox's
3) In TextChanged Event access the controls, find the result logic and assign to the 3rd control. In this case, 3rd control is mentioned as Label. It can be Textbox also.
ASPX Code
<asp:GridView ID="GridView1" runat="server" CellPadding="3" Width="892px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2">
<Columns>
<asp:TemplateField HeaderText="Ales Puanı">
<ItemTemplate>
<asp:TextBox ID="Txt_Ales" runat="server" Text='<%#Eval("alesSinavPuani") %>' OnTextChanged="Txt_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dil Puanı">
<ItemTemplate>
<asp:TextBox ID="txt_Dil" runat="server" Text='<%#Eval("dilSinavPuani") %>' OnTextChanged="Txt_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Toplam">
<ItemTemplate>
<asp:Label ID="lbl_Toplam" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
TextChange Event Code
protected void Txt_TextChanged(object sender, EventArgs e)
{
TextBox Txt_Ales = GridView1.Rows[((sender as TextBox).NamingContainer as GridViewRow).RowIndex].FindControl("Txt_Ales") as TextBox;
TextBox txt_Dil = GridView1.Rows[((sender as TextBox).NamingContainer as GridViewRow).RowIndex].FindControl("txt_Dil") as TextBox;
Label lbl_Toplam = GridView1.Rows[((sender as TextBox).NamingContainer as GridViewRow).RowIndex].FindControl("lbl_Toplam") as Label;
int num1, num2 = 0;
if (Txt_Ales != null && txt_Dil != null && lbl_Toplam != null)
{
int.TryParse(Txt_Ales.Text, out num1);
int.TryParse(txt_Dil.Text, out num2);
lbl_Toplam.Text = (num1 + num2).ToString();
}
}

Access GridView ItemTemplate control ASP.NET

I am using ASP.NET GridView on ASPX page:
<asp:GridView ID="GrdLimitAndUtilization" runat="server" AutoGenerateColumns="False" GridLines="None"
Width="99%" meta:resourcekey="GrdAccountListResource1" OnRowDataBound="GrdLimitAndUtilization_RowDataBound"
ShowHeader="True" rowstyle-cssclass="rowHover" ClientIDMode="Static" CssClass="gridView">
<Columns>
<asp:TemplateField HeaderText="Excess" meta:resourcekey="Excess">
<HeaderStyle HorizontalAlign="Left" Width="120px" CssClass="gridheader" />
<HeaderTemplate> <asp:Label ID="col5a" Text="Excess" runat="server"></asp:Label></HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="lnkExcess" runat="server" value='' Text='<%# Bind("Excess") %>'
meta:resourcekey="HyperLink1Resource1"></asp:HyperLink>
<asp:Label ID="Excess_Currency" runat="server" Text='<%# Bind("Currency") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="120px" HorizontalAlign="Left" CssClass="customerProductItemTemp gridviewLeftMargin" />
</asp:TemplateField>
</Columns>
</asp:GridView>
I know I can access GridView column by following code:
GrdLimitAndUtilization.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Green;
BUT, How can I access value/text of only HyperLink control with ID="lnkExcess"??
Thanks.
You can use RowDataBound event like this:-
protected void GrdLimitAndUtilization_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink lnkExcess = (HyperLink)e.Row.FindControl("lnkExcess");
//Access hyperlink's properties here.
}
}

How to modify this checkbox inside the GridView?

I have the following database design:
Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
Divisions Table: SapCode, DivisionShortcut
And I have a GridView that I am using it to add, delete and update/edit the employees information. This information is employee Username, Name, BadgeNo, JobTitle, IsActive and the DivisionShortcut. IsActive is a flag that indicates if the employee is available or in an assignment. I made it as a checkbox and the column should show two values; Active and Inactive. In the Edit mode, the Checkbox will be displayed. If it is checked, then it means the employee is avaiable, otherwise it is inactive.
I wrote the code and everything works fine, but now I am facing two problems and I don't know how to make them work with my code.
The GridView shows True or False instead of Active and Inactive and
I don't know why.
In the Edit mode, the checkbox will be displayed and the values (Active or Inactive) will be displayed besides to it and it shouldn't be displayed. I just want the checkbox to be displayed.
So how to modify them?
ASP.NET code:
<%-- GridView for User Management Subsystem --%>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="Username"
DataSourceID="SqlDataSource1" BorderWidth="1px" BackColor="#DEBA84"
CellPadding="3" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84">
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510"
HorizontalAlign="Center"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#A55129"></HeaderStyle>
<Columns>
<asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true"
EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png"
CancelImageUrl="Images/icons/cancel324.png" />
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<%# Eval("DivisionShortcut")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource"
DataTextField="DivisionShortcut" DataValueField="SapCode"
SelectedValue='<%# Bind("DivisionCode")%>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Username" HeaderText="Network ID" ReadOnly="True"
SortExpression="Username" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("Name")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title">
<ItemTemplate>
<%# Eval("JobTitle")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Bind("JobTitle")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Badge No.">
<ItemTemplate>
<%# Eval("BadgeNo")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBadgeNo" runat="server" Text='<%# Bind("BadgeNo")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active?">
<ItemTemplate>
<%# Eval("IsActive")%>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server"
AutoPostBack="true" OnCheckedChanged="isActive_OnCheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("IsActive")) %>'
Text='<%# Eval("IsActive").ToString().Equals("True") ? " Active " : " Inactive " %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span onclick="return confirm('Are you sure to Delete the record?')">
<asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" />
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
//for updating the (IsActive) column using checkbox inside the GridView
protected void isActive_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkStatus = (CheckBox)sender;
GridViewRow gvrow = (GridViewRow)chkStatus.NamingContainer;
//Get the ID which is the NetworkID of the employee
string username = gvrow.Cells[2].Text;
bool status = chkStatus.Checked;
string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string updateIsActive = "UPDATE Employee SET IsActive = #IsActive WHERE Username = #Username";
SqlCommand cmd = new SqlCommand(updateIsActive, conn);
cmd.Parameters.AddWithValue("#IsActive", status);
cmd.Parameters.AddWithValue("#Username", username);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (SqlException se)
{
throw se;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
UPDATE:
I update my code as following:
ASP.NET Code:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="Username"
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" BorderWidth="1px" BackColor="#DEBA84"
CellPadding="3" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84">
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510"
HorizontalAlign="Center"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#A55129"></HeaderStyle>
<Columns>
<asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true"
EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png"
CancelImageUrl="Images/icons/cancel324.png" />
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<%# Eval("DivisionShortcut")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource"
DataTextField="DivisionShortcut" DataValueField="SapCode"
SelectedValue='<%# Bind("DivisionCode")%>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Username" HeaderText="Network ID" ReadOnly="True"
SortExpression="Username" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("Name")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title">
<ItemTemplate>
<%# Eval("JobTitle")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Bind("JobTitle")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Badge No.">
<ItemTemplate>
<%# Eval("BadgeNo")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBadgeNo" runat="server" Text='<%# Bind("BadgeNo")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active?">
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%# Eval("IsActive")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server"
AutoPostBack="true" OnCheckedChanged="isActive_OnCheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("IsActive")) %>'
Text='<%# Eval("IsActive")%>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span onclick="return confirm('Are you sure to Delete the record?')">
<asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" />
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-Behind:
//For showing Active or Inactive in the IsActive column instead of True or False
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Here we will select the IsActive column and modify its text
Label isActive = new Label();
isActive = (Label)e.Row.FindControl("lblIsActive");
//Now, after getting the reference, we can set its text
isActive.Text = "Active or Inactive";
// First check Checkedbox is check or not, if not make it grey
//if (e.Row.Cells[6].Text == "False")
// GridView1.BackColor = Color.Gray;
}
}
And I am getting the following error and I don't know why:
For question no: 1
You have to add an Row_DataBound Event of your grid view, in which you select the IsActive column and replaced its text by Active and InActive. But before going to code behind make a little change to you aspx code: In your item template instead of direct binding place a label control and bind it as I does in the below code:
<asp:TemplateField HeaderText="Is Active?">
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%# Eval("IsActive") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server"
AutoPostBack="true" OnCheckedChanged="isActive_OnCheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("IsActive")) %>'
Text='<%# Eval("IsActive").ToString().Equals("True") ? " Active " : " Inactive " %>'/>
</EditItemTemplate>
</asp:TemplateField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// here you will select the IsActive column and modify it's text
Label isActive=new Label();
isActive = (Label) e.row.FindControl("lblIsActive");
// after getting the reference set its text
isActive.Text="Active or InActive";
}
}
For question no 2:
Remove the condition form this
Eval("IsActive").ToString().Equals("True") ? " Active " : " Inactive " %>'
and replace it with
Eval("IsActive")
Now check boxes will be displayed.
Updated Answer:
You are receiving an object reference not found error, Please debug your site and check it why you are unable to get the exact reference. I saw you code and it looks that your code is fine.
To prove that my code is working,I created a gridview with a single column
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
// and bind this grid view in the Page_Load of my Page
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("name");
DataRow row = dt.NewRow();
row[0] = "Waqar Janjua";
dt.Rows.Add(row);
GridView1.DataSource = dt;
GridView1.DataBind();
}
// When I view this page in the browser it shows "Waqar Janjua"
// Now to update the columns text, I add the following code and it works.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState != DataControlRowState.Edit)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label l = new Label();
l = (Label)e.Row.FindControl("Label1");
l.Text = "waqar";
}
}
}
// When I run this page now, it shows me "Waqar" not "Waqar Janjua" The code works for me.
It is returning "true" or "false" as it is expecting a boolean data type from IsActive. If you want to change that out put, I suspect you need to amend this conditionally on the ItemDataBound event of the grid.

Hidden fields in a grid won't import to a edit form

I've got a grid that only displays one piece of information such as a title. Other fields are hidden. When you click edit a modal popup displays a form and imports the information from the grid for editing. The information in hidden field are not imported though. I don't want to display all the information in the grid because I have only room for the title.
How can I make this work? Thanks. Risho
<asp:GridView ID="gvForum" runat="server" DataSourceID="odsForumApproval" DataKeyNames="id" Width="200px"
RepeatColumns="1" DataKeyField="id" CssClass="gridview"
AutoGenerateColumns="False" GridLines="None" OnSelectedIndexChanged="_OnCommand">
<AlternatingRowStyle CssClass="altbgcolor" />
<Columns>
<asp:BoundField DataField="title" />
<asp:TemplateField >
<ItemTemplate>
<asp:HiddenField runat="server" ID="hfId" Value='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:HiddenField runat="server" ID="hfDesc" Value='<%# Eval("description") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="lnkbtn" Text="Approve" runat="server" onclick="lnkbtn_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
protected void lnkbtn_Click(object sender, EventArgs e)
{
LinkButton btndetails = sender as LinkButton;
GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
lblID.Value = gvrow.Cells[1].Text;
txtTitle.Text = gvrow.Cells[0].Text;
lblMessage.Text = gvrow.Cells[3].Text;
this.ModalPopupExtender1.Show();
}
The Cell-Text is empty if you are using TemplateFields with nested controls . You need to get the reference to the controls and use their appropriate properties(like TextBox.Text or HiddenField.Value). Therefor you can use FindControl on the GridViewRow:
var hfId = (HiddenField)gvrow.FindControl("hfId");
var hfDesc = (HiddenField)gvrow.FindControl("hfDesc");
txtTitle.Text = gvrow.Cells[0].Text;
lblID.Value = hfId.Value;
txtTitle.Text = hfdesc.Value;

Categories

Resources