Can't get the anything from e.Item.Cells[1].Text
-
I am creating an asp.net site with c# as the backend. In this page, I have created a datagrid which uses TemplateColumns instead of BoundColumns. The problem I am having is that when I select a column with the edit button, I can't get a value back from e.Item.Cells[1].Text I can do the same exact thing in VB and it works fine though. Including a code sample which I made just to try and track down the issue. In the below code, I have a delete button that gives a pop-up listing for each e.Item.Cells[x].Text. The BoundColumn works, while the TemplateColumn does not work. Here is the datagrid: <%# DataBinder.Eval(Container.DataItem, "AssetTag") %> And here is the code behind: namespace SUSExemption { using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Configuration; using System.Web.Mail; /// /// Summary description for ucForm_Admin2. /// public class ucForm_Admin2 : System.Web.UI.UserControl { protected System.Web.UI.WebControls.Label lblUserLogin; protected System.Web.UI.WebControls.TextBox txtUserLogin; protected System.Web.UI.WebControls.Button btnUserSearch; protected System.Web.UI.WebControls.Label lblUserError; protected System.Web.UI.WebControls.Label lblUserName; protected System.Web.UI.WebControls.TextBox txtUserName; protected System.Web.UI.WebControls.Label lblUserManagerLogi
-
I am creating an asp.net site with c# as the backend. In this page, I have created a datagrid which uses TemplateColumns instead of BoundColumns. The problem I am having is that when I select a column with the edit button, I can't get a value back from e.Item.Cells[1].Text I can do the same exact thing in VB and it works fine though. Including a code sample which I made just to try and track down the issue. In the below code, I have a delete button that gives a pop-up listing for each e.Item.Cells[x].Text. The BoundColumn works, while the TemplateColumn does not work. Here is the datagrid: <%# DataBinder.Eval(Container.DataItem, "AssetTag") %> And here is the code behind: namespace SUSExemption { using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Configuration; using System.Web.Mail; /// /// Summary description for ucForm_Admin2. /// public class ucForm_Admin2 : System.Web.UI.UserControl { protected System.Web.UI.WebControls.Label lblUserLogin; protected System.Web.UI.WebControls.TextBox txtUserLogin; protected System.Web.UI.WebControls.Button btnUserSearch; protected System.Web.UI.WebControls.Label lblUserError; protected System.Web.UI.WebControls.Label lblUserName; protected System.Web.UI.WebControls.TextBox txtUserName; protected System.Web.UI.WebControls.Label lblUserManagerLogi
You will need to access the DataBoundLiteralControl[^] in the same manner you have the LinkButton. The templated column is different from the bound column in that is implements the control collection for displaying its contents. In order to access the value of the templated column, you will need to evaluate the appropiate member of its control collection. =============== Sample DataGrid Code
<asp:DataGrid ID="dgAdmin" Runat="server" DataKeyField="Id" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Id" HeaderText="Id" />
<asp:BoundColumn DataField="Value1" HeaderText="Value1" />
<asp:BoundColumn DataField="Value2" HeaderText="Value2" />
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" Runat="server">Delete
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>=============== Event Code
private void dgAdmin_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.EditItem)
{
//## First, make sure we're NOT dealing with a Header or Footer row
if (e.Item.ItemType != ListItemType.Header)
{
if (e.Item.ItemType != ListItemType.Footer)
{
//## Now, reference the LinkButton control that the Delete ButtonColumn has been rendered to
System.Web.UI.WebControls.LinkButton deleteButton;
deleteButton = (LinkButton)e.Item.FindControl("lnkDelete");//## ADDITIONAL CODE System.Web.UI.DataBoundLiteralControl templatedColumn; templatedColumn = (DataBoundLiteralControl)e.Item.Cells\[2\].Controls\[0\]; //## We can now add the onclick event handler //deleteButton.Attributes\["onclick"\] = "javascript:return confirm('Template Column: " + e.Item.Cells\[1\].Text + " | DataBound Column: " + e.Item.Cells\[2\].Text +"');"; //## MODIFIED CODE deleteButton.Attributes\["onclick"\] = "javascript:return confirm('Template Column: " + e.Item.Cells\[1\].Text + " | DataBound Column: " + templatedColumn.Text +"');"; } } }
}