I just attached linq data to gridview it is not showing row ?
DataDataContext db = new DataDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindData();
}
private void BindData()
{
var source = from n in db.Names
select n;
gridSample.DataSource = source.ToList<Name>();
gridSample.DataBind();'
}
<asp:UpdatePanel ID="panelGrid" runat="server">
<ContentTemplate>
<asp:GridView runat="server" ID="gridSample" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="First Name">
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%Bind("FirstName") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblFirstName" Text='<%Eval("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%Bind("LastName") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblLastName" Text='<%Eval("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Birth">
<EditItemTemplate>
<asp:TextBox ID="txtDOB" runat="server" Text='<%Bind("DOB") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblDOB" Text='<%Eval("DOB") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Try this
List<Name> source = (from n in db.Names select n).ToList();
gridSample.DataSource = source;
gridSample.DataBind();'
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' />
try this:
private void BindData()
{
var source = from n in db.Names
select n;
gridSample.DataSource = source;
gridSample.DataBind();'
}
Regards
Set the property
AutoPostBack = True
for the GridView. Since it is inside the UpdatePanel.
Related
I have an editable GridView but I would also like to have an add new row functionality, so I made a button using FooterTemplate for all editable fields and set CommandName="AddNew" . Everything looks as expected on the front end, but Add New Row cannot find txtGrantId, txtPotId or txtBudget at all and throws System.NullReferenceException 'Object reference not set to an instance of an object.'. The issue has to be in the code behind, but I am attaching the front end as well. The data source is pretty long but it does call OnRowCommand="gvPotsMoneyGrants_RowCommand"
ASPX
<%--Primary Key LinkId--%>
<asp:TemplateField HeaderText="LinkId" SortExpression="LinkId">
<ItemTemplate>
<asp:Label ID="lblLinkId" runat="server" Text='<%# Eval("LinkId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%--GrantId--%>
<asp:TemplateField HeaderText="GrantId" SortExpression="GrantId">
<ItemTemplate>
<asp:Label ID="lblGrantIdMain" runat="server" Text='<%#Eval("GrantId") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGrantId" runat="server" Text='<%#Bind("GrantId") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inGrantId" Width="120px" runat="server" />
<asp:RequiredFieldValidator ID="vGrantId" runat="server" ControlToValidate="inGrantId" Text="?" ValidationGroup="VG5" />
</FooterTemplate>
</asp:TemplateField>
<%--PotId--%>
<asp:TemplateField HeaderText="PotId" SortExpression="PotId">
<ItemTemplate>
<asp:Label ID="lblPotIdMain" runat="server" Text='<%#Eval("PotId") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPotId" runat="server" Text='<%#Bind("PotId") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inPotId" Width="120px" runat="server" />
<asp:RequiredFieldValidator ID="vPotId" runat="server" ControlToValidate="inPotId" Text="?" ValidationGroup="VG5" />
</FooterTemplate>
</asp:TemplateField>
<%--Budget--%>
<asp:TemplateField HeaderText="Budget" SortExpression="Budget">
<ItemTemplate>
<asp:Label ID="lblBudgetMain" runat="server" Text='<%#Eval("Budget") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBudget" runat="server" Text='<%#Bind("Budget") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inBudget" Width="120px" runat="server" />
<asp:RequiredFieldValidator ID="vBudget" runat="server" ControlToValidate="inBudget" Text="?" ValidationGroup="VG5" />
</FooterTemplate>
</asp:TemplateField>
Code behind
protected void gvPotsMoneyGrants_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtGrantId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtGrantId");
TextBox txtPotId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtPotId");
TextBox txtBudget = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtBudget");
string GrantId, PotId, Budget;
GrantId = txtGrantId.Text;
PotId = txtPotId.Text;
Budget = txtBudget.Text;
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("GrantId", GrantId);
cmd.Parameters.AddWithValue("PotId", PotId);
cmd.Parameters.AddWithValue("Budget", Budget);
}
}
Please, try the code below.
protected void gvPotsMoneyGrants_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox inGrantId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inGrantId");
TextBox inPotId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inPotId");
TextBox inBudget = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inBudget");
string GrantId, PotId, Budget;
GrantId = inGrantId.Text;
PotId = inPotId.Text;
Budget = inBudget.Text;
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("GrantId", GrantId);
cmd.Parameters.AddWithValue("PotId", PotId);
cmd.Parameters.AddWithValue("Budget", Budget);
}
}
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">
This is the code where I have problem, I don't have any problems in the previous page but this is happening in this and following page.
<asp:GridView ID="gv_employee" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false" OnRowEditing="Edit" OnRowCancelingEdit="CancelEdit" OnRowUpdating="Update" Width="1070px"
AlternatingRowStyle-BackColor="WhiteSmoke" HeaderStyle-BackColor="#C5D9F1">
<Columns>
<asp:TemplateField HeaderText="S.No" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="60px">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Code" ItemStyle-Width="160px">
<ItemTemplate>
<asp:Label ID="lbl_gv_empcode" runat="server" Text='<%#Eval("employeecode")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:Label ID="lbl_gv_empname" runat="server" Text='<%#Eval("employeename") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_empname" runat="server" Text='<%#Eval("employeename") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address1">
<ItemTemplate>
<asp:Label ID="lbl_gv_addr1" runat="server" Text='<%#Eval("address1") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_addr1" runat="server" Text='<%#Eval("address1") %>' />
</EditItemTemplate>
</asp:TemplateField>
<%-- <asp:TemplateField HeaderText="Address2">
<ItemTemplate>
<asp:Label ID="lbl_gv_addr2" runat="server" Text='<%#Eval("address2") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_addr2" runat="server" Text='<%#Eval("address2") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address3">
<ItemTemplate>
<asp:Label ID="lbl_gv_addr3" runat="server" Text='<%#Eval("address3") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_addr3" runat="server" Text='<%#Eval("address3") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lbl_gv_city" runat="server" Text='<%#Eval("city")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_city" runat="server" Text='<%#Eval("city")%>' />
</EditItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lbl_gv_email" runat="server" Text='<%#Eval("email") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_email" runat="server" Text='<%#Eval("email") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label ID="lbl_gv_phone" runat="server" Text='<%#Eval("phone") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_phone" runat="server" Text='<%#Eval("phone") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btndel" runat="server" Text="Delete" CommandArgument='<%#Eval("employeecode") %>'
OnClientClick="return confirm('Do you want to delete?')" OnClick="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is my asp code
public void BindData()
{
string ls_sqlcmd = "select * from employeemst";
SqlCommand cmd = new SqlCommand(ls_sqlcmd);
gv_employee.DataSource = add.GetData(cmd);
gv_employee.DataBind();
}
protected void Delete(object sender, EventArgs e)
{
LinkButton lnk_deletestate = (LinkButton)sender;
string str = "delete from employeemst where employeecode='" + lnk_deletestate.CommandArgument + "'";
add.adddata(str);
BindData();
}
protected void Update(object sender, GridViewUpdateEventArgs e)
{
String emplname = ((TextBox)gv_employee.Rows[e.RowIndex].FindControl("txt_gv_empname")).Text;
String Addr1= ((TextBox)gv_employee.Rows[e.RowIndex].FindControl("txt_gv_addr1")).Text;
String addr2 = ((TextBox)gv_employee.Rows[e.RowIndex].FindControl("txt_gv_addr2")).Text;
String addr3 = ((TextBox)gv_employee.Rows[e.RowIndex].FindControl("txt_gv_addr3")).Text;
gv_employee.EditIndex = -1;
BindData();
}
protected void Edit(object sender, GridViewEditEventArgs e)
{
gv_employee.EditIndex = e.NewEditIndex;
BindData();
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
gv_employee.EditIndex = -1;
BindData();
}
}
This is my aspx.cs code.
My problem is that I am unable to get data from textbox which is in gridview <EditItemTemplate></EditItemTemplate>
I don't get updated value in string emplname, addr1, addr2,addr3.
your issue is you are binding grid view in each postback, move your BindData method inside the IsPostBack check, then you can get the data
protected void Page_Load(object sender, EventArgs e)
{
Session["page"] = "Employee";
if (!IsPostBack)
{
BindData();
DataTable dt = add.retrive("select * from statemst");
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem item = new ListItem();
item.Text = dt.Rows[i]["statedesc"].ToString();
item.Value = dt.Rows[i]["statecode"].ToString();
ddl_state.Items.Add(item);
}
}
}
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
I am trying to get a gridview to populate text from the database call to my Label as shown
The Results have been tested and are returning the correct names
protected void Page_Load(object sender, EventArgs e)
{
DataTable t = DBProductLink.ListWithOptions(ProductId, LinkType, null);
TestList.DataSource = t ;
TestList.DataBind();
}
The labels are created in the Gridview like this:
<asp:GridView ID="TestList" runat="server" OnRowDataBound="testDataBound" AutoGenerateColumns="false" DataKeyNames="Id">
<Columns>
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I then tried to loop through the gridview and access the label using the ondatarowbound, however in this it is null.
protected void testDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
Label sizeLabel = e.Row.FindControl("sizeLabel") as Label;
sizeLabel.Text = "test";
}
I am using the exact same set up with 2 drop boxes and 2 labels on another gridview with a different name on the same page which is not having this problem. Anyone got an idea on this?
The other Gridview is as follows:
<asp:GridView ID="SearchList" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" OnRowDataBound="SearchList_RowDataBound"
OnRowCommand="SearchList_RowCommand" Width="100%" PageSize="20" >
<Columns>
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<ItemTemplate>
<robo:MoneyLabel ID="MoneyLabel2" runat="server"
Value='<%# Eval("Price") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label ID="typeLabel" runat="server" Text='<%# Eval("Type") %>' />
<asp:HiddenField ID="productId" runat="server" Value='<%# Eval("Id") %>' />
<asp:HiddenField ID="isFabric" runat="server" Value='<%# Eval("IsFabric") %>' />
<asp:HiddenField ID="isOldWizard" runat="server" Value='<%# Eval("IsOldWizard") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options/Color/Size">
<ItemTemplate>
<asp:LinkButton runat="server" ID="GetOptions" Text="Get Options" CausesValidation="false" CommandName="Options" />
<asp:Label ID="OptionLabel" Visible="false" runat="server" Text="Option: " />
<asp:DropDownList ID="ProductOptions" runat="server" Visible="false" />
<asp:Label ID="ColorLabel" Visible="false" runat="server" Text="Color: " />
<asp:DropDownList ID="RibbonColors" runat="server" Visible="false" AutoPostBack="true" />
<asp:Label ID="SizeLabel" Visible="false" runat="server" Text="Size: " />
<asp:DropDownList ID="RibbonSizes" runat="server" Visible="false" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Add">
<ItemStyle Width="60px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs is
protected void SearchList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
int productId = (int)SearchList.DataKeys[e.Row.RowIndex].Value;
LinkButton GetOptions = e.Row.FindControl("GetOptions") as LinkButton;
DropDownList RibbonColors = e.Row.FindControl("RibbonColors") as DropDownList;
DropDownList RibbonSizes = e.Row.FindControl("RibbonSizes") as DropDownList;
DropDownList ProductOptions = e.Row.FindControl("ProductOptions") as DropDownList;
Label typeLabel = e.Row.FindControl("typeLabel") as Label;
HiddenField isFabric = e.Row.FindControl("isFabric") as HiddenField;
HiddenField isOldWizard = e.Row.FindControl("isOldWizard") as HiddenField;
ProductType typeValue = DBConvert.ToEnum<ProductType>(typeLabel.Text);
bool isFabricValue = Convert.ToBoolean(isFabric.Value.ToString());
bool isOldWizardValue = Convert.ToBoolean(isOldWizard.Value.ToString());
}
I just found the problem
Your markup is wrong it was tricky... I admit it
This tag: <asp:ItemTemplate> should be <ItemTemplate>
Change this:
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
Into
<asp:TemplateField HeaderText="Sizes">
<ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</ItemTemplate>
</asp:TemplateField>
This should raise an exception... but instead, the ItemTemplate was completely ignored