I have a gridview with many columns and rows. I need to get the Checkbox Status for each one of the Template fields from the gridview.
I know I can achieve this by getting each individual:
CheckBox cbTrans = (CheckBox)gvStations.Rows[rowindex].FindControl("cbTaxEdit");
CheckBox cbReg = (CheckBox)gvStations.Rows[rowindex].FindControl("cbRegEdit");
If I have 20 template fields how can I get all of them one at a time. My idea is to get one a time in a for loop and perform an operation for each checkbox.
//Something along this lines but I know I can't use J here
for (int i = 0; i <= this.gvStations.Rows.Count - 1; i++)
{
for (int j = 0; j <= this.gvSta.Columns.Count - 1; j++)
{
//I WANT TO GO THRU EACH COLUMN, J can't be used here, throws error
CheckBox cbox = (CheckBox)gvStations.Rows[i].FindControl(j);
if(cbox.Checked)
{
//Perform Operation
}
}
}
Gridview
<asp:TemplateField HeaderText="TAX" SortExpression="Taxes">
<EditItemTemplate>
<asp:CheckBox ID="cbTaxEdit" runat="server" Checked='<%# (int)Eval("Taxes") == 1 %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbTax" runat="server" Enabled="false" Checked='<%# (int)Eval("Taxes") == 1 %>'>
</asp:CheckBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<%--Registrations--%>
<asp:TemplateField HeaderText="REG" SortExpression="Registrations">
<EditItemTemplate>
<asp:CheckBox ID="cbRegEdit" runat="server" Checked='<%# (int)Eval("Registrations") == 1 %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbReg" runat="server" Enabled="false" Checked='<%# (int)Eval("Registrations") == 1 %>'>
</asp:CheckBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="BTRColumn" SortExpression="BTR">
<EditItemTemplate>
<asp:CheckBox ID="cbBTREdit" runat="server" Checked='<%# (int)Eval("BTR") == 1 %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbBTR" runat="server" Enabled="false" Checked='<%# (int)Eval("BTR") == 1 %>'>
</asp:CheckBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
Posted it as a comment but meant to post it as an answer haha! Been a long day.
I would loop through the check boxes and gather the IDs for them in a list. Lets call list "CheckBoxIDs". I would then do this:
CheckBox cbox = (CheckBox)gvStations.Rows[i].FindControl(CheckBoxIDs[j]);
Related
I am extracting data from database in gridview, where I have given a link button "Edit" for editing the data of that row. However while editing , one of the columns(YEAR) of my gridview becomes a dropdown list. While fetching the data from database, the Column "Year" consists of label and in edit mode it gets changed to dropdownlist. Moreover I added a condition to make the link buttons appear/disappear based on the value of the Label "Year". When I edit the particular row it throws an error because as soon as edit command is called it converts the column into dropdownlist however the row_databound searches for label. It shows "Object not set to an instance....." PLease help
Aspx
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource2" OnRowDataBound="gv1_RowDataBound" OnRowUpdating="gv1_RowUpdating" DataKeyNames="tid" CssClass="auto-style1" Width="694px">
<Columns>
<asp:TemplateField HeaderText="Sr No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="tid" SortExpression="tid" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("tid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" SortExpression="title">
<EditItemTemplate>
<asp:TextBox ID="txtTitle1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="category">
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server" Text='<%# Bind("category") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="rbtnlist" runat="server"><asp:ListItem Text="Soft Skills"></asp:ListItem><asp:ListItem Text="Technical Skills"></asp:ListItem></asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Announced at" SortExpression="dt">
<ItemTemplate>
<asp:Label ID="lblDt" runat="server" Text='<%# Bind("dt", "{0:dd/MMM/yyyy HH:mm tt}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year" SortExpression="year">
<EditItemTemplate>
<asp:DropDownList ID="ddlyr" runat="server"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblyr" runat="server" Text='<%# Bind("year") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Id" SortExpression="userid">
<ItemTemplate>
<asp:Label ID="lblUid" runat="server" Text='<%# Bind("userid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="btndel" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return isConfirm()" Visible="false"></asp:LinkButton>
<%--<asp:LinkButton ID="btnmail" runat="server" CausesValidation="False" OnClientClick="return isConfirm()" Text="Send Mail" Visible="false"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="btnupdte" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="btncncl" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" Visible="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnmail" runat="server" CausesValidation="False" OnClientClick="return isConfirm()" Text="Send Mail" Visible="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
c#
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbly = (Label)e.Row.FindControl("lblyr");
if (lbly.Text == DateTime.Now.Year.ToString() || lbly.Text == ((DateTime.Now.Year)-1).ToString())
{
LinkButton btedt = (LinkButton)e.Row.FindControl("btnedit");
LinkButton btdel = (LinkButton)e.Row.FindControl("btndel");
LinkButton btsm = (LinkButton)e.Row.FindControl("btnmail");
btdel.Visible = true;
btedt.Visible = true;
btsm.Visible = true;
}
if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
RadioButtonList rbtnlist = (RadioButtonList)e.Row.FindControl("rbtnlist");
DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlyr");
for (int i = (DateTime.Now.Year); i >= ((DateTime.Now.Year)-1) ; i--)
{
ddlist.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlist.DataBind();
rbtnlist.DataBind();
}
}
}
I am assuming that you are binding your grid with a DataTable with colum name "Year". Try below code where dt refers to your Datatable:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int rowno = e.Row.DataItemIndex;
//Suppose dt is DataTable to which you are binding your grid
//and year is the column name within this datatable
string year = Convert.ToString(dt.Rows[e.Row.DataItemIndex]["year"]);
if (year == DateTime.Now.Year.ToString() || year == ((DateTime.Now.Year) - 1).ToString())
{
LinkButton btedt = (LinkButton)e.Row.FindControl("btnedit");
LinkButton btdel = (LinkButton)e.Row.FindControl("btndel");
LinkButton btsm = (LinkButton)e.Row.FindControl("btnmail");
btdel.Visible = true;
btedt.Visible = true;
btsm.Visible = true;
}
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
RadioButtonList rbtnlist = (RadioButtonList)e.Row.FindControl("rbtnlist");
DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlyr");
for (int i = (DateTime.Now.Year); i >= ((DateTime.Now.Year) - 1); i--)
{
ddlist.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlist.DataBind();
rbtnlist.DataBind();
}
}
I face a strange behavior when i make the default for check box in the gridview is Checked = true !!
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:Panel ID="pnl_lect" runat="server">
<asp:GridView ID="gv_lect" runat="server" AutoGenerateColumns="False" DataKeyNames="prog_ser,lect_code,lect_type"
CssClass="formTable cr_center" OnDataBound="gv_lect_DataBound" OnRowDataBound="gv_lect_RowDataBound">
<Columns>
<asp:BoundField HeaderText="lect_name" DataField="name" />
<asp:TemplateField HeaderText="1">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_1" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="2">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_2" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="3">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_3" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="4">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_4" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="5">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_5" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="6">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_6" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="7">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_7" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="8">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_8" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="9">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_9" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="10">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_10" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="11">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_11" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="12">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_12" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="13">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_13" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="14">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_14" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="15">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_15" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="16">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_16" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="17">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_17" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="18">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_18" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="19">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_19" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="20">
<ItemTemplate>
<asp:CheckBox ID="CheckBox_20" runat="server" Checked="false" AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
bool res = true;
try
{
CheckBox chkbox = (CheckBox)sender;
GridViewRow r = (GridViewRow)chkbox.NamingContainer;
ProgAbsence abs = new ProgAbsence();
int progSer = int.Parse(((GridView)(r.NamingContainer)).DataKeys[r.DataItemIndex].Values[0].ToString());
int lectCode = int.Parse(((GridView)(r.NamingContainer)).DataKeys[r.DataItemIndex].Values[1].ToString());
int lectType = int.Parse(((GridView)(r.NamingContainer)).DataKeys[r.DataItemIndex].Values[2].ToString());
int sessionId = int.Parse(chkbox.ID.Split('_')[1]);
abs.LectCode = lectCode;
abs.LectType = lectType;
abs.ProgSer = progSer;
abs.SessionId = sessionId;
if (chkbox.Checked)
{
res = abs.Add();
}
else
{
res = abs.Delete();
}
BindAbsence();
}
catch (Exception ee)
{
res = false;
BindAbsence();
}
}
/* =========================================================================== */
protected void gv_lect_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItemIndex >= 0)
{
DataTable dt = ((DataTable)ViewState["abs"]);
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (((GridView)(e.Row.NamingContainer)).DataKeys[e.Row.DataItemIndex].Values[0].ToString().Trim() == dt.Rows[i]["prog_ser"].ToString()
&&
((GridView)(e.Row.NamingContainer)).DataKeys[e.Row.DataItemIndex].Values[1].ToString().Trim() == dt.Rows[i]["lect_code"].ToString()
&&
((GridView)(e.Row.NamingContainer)).DataKeys[e.Row.DataItemIndex].Values[2].ToString().Trim() == dt.Rows[i]["lect_type"].ToString())
{
((CheckBox)(e.Row.Cells[int.Parse(dt.Rows[i]["session_id"].ToString())].Controls[1])).Checked = true;
}
}
}
}
}
/* =========================================================================== */
This code works fine but when i make all the check boxes checked property default value is true instead of false , i get wrong checkbox(not the sender) and the event works sometimes not all the time !!
How to make the checkbox default value is true and make it works as expected ?
Did you set EnableViewState to false in the Page directive or in a control containing your GridView?
<%# Page EnableViewState="false" ... %>
The CheckBox.CheckedChanged Event might not work properly without this set to true. MSDN says:
A CheckBox control must persist some values between posts to the server for this event to work correctly. Be sure that view state is enabled for this control.
Kindly test the said scenario in the new solution and instead of binding the gridview from database, try to bind it using defined data(DataTable with values). and From there implement your code piece by piece.
Here is checklist you should go thuru on this list..
Make sure you enabled ViewState on page
You are using update panel so make Children As Trigger property to true
Make sure AutoPostBack=true
If you binding your Gridview on Page_load Put Binding in if(!IsnotPostBack) {}
Make sure you not check or made change on checkbox using javascript
If you still have a problem create new page and try with only grid-view It might be other issue..
I have a GridView that displays records that are either the number 1 or 0.
How can I convert the record into a check box which is checked if the value is 1 or unchecked if the value is 0?
<asp:GridView ID="gvStations" runat="server">
<columns>
<asp:templatefield headertext="Type" sortexpression="TypeDesc">
<edititemtemplate>
<asp:CheckBox ID="cbTypeEdit" runat="server" Text='<%# Bind("TypeDesc") />
</edititemtemplate>
<itemtemplate>
<asp:CheckBox ID="cbType" runat="server" Text='<%# Bind("TypeDesc") ></asp:CheckBox>
</itemtemplate>
<itemstyle horizontalalign="Center" />
</asp:templatefield>
<asp:templatefield headertext="Type 2" sortexpression="TypeDesc2">
<edititemtemplate>
<asp:CheckBox ID="cbType2Edit" runat="server" Text='<%# Bind("TypeDesc2") />
</edititemtemplate>
<itemtemplate>
<asp:CheckBox ID="cbType2" runat="server" Text='<%# Bind("TypeDesc2") ></asp:CheckBox>
</itemtemplate>
<itemstyle horizontalalign="Center" />
</asp:templatefield>
</columns>
</asp:GridView>
Right now I can change them to an X if its a 1 and a "blank" if its a 0.
protected void gvStations_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i <= this.gvStations.Rows.Count - 1; i++)//from here down changes the 1's to X's and O's to blanks.
{
for (int j = 0; j <= this.gvStations.Columns.Count - 1; j++)
{
if (j != 1 && j != 2)
{
if (this.gvStations.Rows[i].Cells[j].Text == "1")
{
this.gvStations.Rows[i].Cells[j].Text = "X";
}
else if (this.gvStations.Rows[i].Cells[j].Text == "0")
{
this.gvStations.Rows[i].Cells[j].Text = " ";
}
}
}
}
}
For display you can go as simple as a single comparison of the binded value:
<asp:CheckBox ID="cbTypeEdit" runat="server"
Checked='<%# (int)Eval("TypeDesc") == 1 %>'/>
For handling editing of the grid row I am afraid you need to handle the GridView's RowUpdated event and convert the values manually.
I think that should be:
<asp:CheckBox ID="cbTypeEdit" runat="server" Text='<%# Bind("TypeDesc") %>' Checked="<%# (Int)Eval("TypeDesc") == 0 ? false : true %>" />
A simple helper function would be better as you have TypeDesc and TypeDesc2.
Markup
Checked='<%# SetCheckedStatus(Bind("TypeDesc")) %>'
Checked='<%# SetCheckedStatus(Bind("TypeDesc2")) %>'
Code-behind
protected bool SetCheckedStatus(object typeDesc)
{
var isChecked = false;
var intValue = 0;
if (typeDesc != null)
{
int.TryParse(typeDesc.ToString(), out intValue);
if (intValue == 1) { isChecked = true; }
}
return isChecked;
}
Much cleaner markup this way
I have a footer template in my gridview which allows users to add new items to the grid. Since they don't want to go below the page each time to add and they want to bring that footer row as the first row of the gridview.
ASPX:
<asp:GridView ID="gvApplication" runat="server" AutoGenerateColumns="False" HorizontalAlign="center"
ShowFooter="True" CellPadding="3" GridLines="Both" CssClass="contentfont" ShowHeaderWhenEmpty="True"
EmptyDataText="No Records Found" OnRowDataBound="gvApplication_RowDataBound"
OnRowDeleting="gvApplication_RowDeleting" OnRowCommand="gvApplication_RowCommand"
DataKeyNames="ID,Group,App_Name" Width="100%">
<Columns>
<asp:TemplateField HeaderText="ID"
<asp:Label ID="lblID" runat="server" Text='<%# Bind("[ID]") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlID" runat="server" Width="150px" AutoPostBack="True"
OnSelectedIndexChanged="ddlID_SelectedIndexChanged" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Application Group">
<ItemTemplate>
<asp:Label ID="lblAppGrp" runat="server" Text='<%# Bind("[Group]") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlGroup" runat="server" Width="150px" AutoPostBack="True"
OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Application Name">
<ItemTemplate>
<asp:Label ID="lblAppName" runat="server" Text='<%# Bind ("[App_Name]") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlApp" runat="server" Width="150px" AutoPostBack="True"
OnSelectedIndexChanged="ddlApp_SelectedIndexChanged" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/delete.gif"
ToolTip="Delete" OnClientClick="return confirm('Are you sure want to Delete');" />
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" Width="65px" runat="server" CausesValidation="False"
CommandName="AddNew" Text="Link Code" ForeColor="#3f6da2" Font-Bold="true">
</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<RowStyle Wrap="False" />
<HeaderStyle Wrap="False" BackColor="#5B95CF" ForeColor="White" Height="25px" BorderStyle="Ridge" />
<FooterStyle BackColor="#D1DDF1" ForeColor="White" Font-Bold="True" />
</asp:GridView>
CS:
public void gvApplication_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlID = (DropDownList)e.Row.FindControl("ddlID");
ddlID .DataSource = AppDataRepository.GetID();
ddlID .DataValueField = "ID";
ddlID .DataTextField = "ID";
ddlID .DataBind();
ddlID .Items.Insert(0, new ListItem("--Select ID--", "0"));
ddlID .SelectedIndex = 0;
DropDownList ddlGroup = (DropDownList)e.Row.FindControl("ddlGroup");
ddlGroup.DataSource = AppDataRepository.GetGroup();
ddlGroup.DataValueField = "Group";
ddlGroup.DataTextField = "Group";
ddlGroup.DataBind();
ddlGroup.Items.Insert(0, new ListItem("--Select Group--", "0"));
ddlGroup.SelectedIndex = 0;
DropDownList ddlApp = (DropDownList)e.Row.FindControl("ddlApp");
ddlApp.DataSource = AppDataRepository.GetApp();
ddlApp.DataValueField = "App_Name";
ddlApp.DataTextField = "App_Name";
ddlApp.DataBind();
ddlApp.Items.Insert(0, new ListItem("--Select App--", "0"));
ddlApp.SelectedIndex = 0;
}
}
Try with HeaderTemplate
<asp:TemplateField HeaderText="ID"
<asp:Label ID="lblID" runat="server" Text='<%# Bind("[ID]") %>'></asp:Label>
</ItemTemplate>
<HeaderTemplate>
<asp:DropDownList ID="ddlID" runat="server" Width="150px" AutoPostBack="True"
OnSelectedIndexChanged="ddlID_SelectedIndexChanged" />
</HeaderTemplate>
</asp:TemplateField>
add FooterStyle-HorizontalAlign="Right" to the parent column of the template
Since Internet explorer dropped support for css top:expression - I have had to separate gridview headers into a separate table.
I have a table. In the first row / first cell I put another table containing the table headers.
In the next row / first cell I have a div set to be 20px wider than the table above (to allow for scroll bar on div). The div contains the gridview with no header. In the pre_render of the gridview I set the widths of the columns in both tables.
I spend a bit of time checking the size of the user screen etc and setting widths for the the tables / div (and a height for the div). The result is a table with static headers which are exactly the same width as the gridview below. You could easily do this and add an extra row in the header table to contain the controls that are currently in the footer row of the gridview.
I have a dropdown down after postback i want to bind the second dropdown . i get the value but that is not displayed in the second data:EntityDropDownList .
<ItemTemplate>
<data:EntityDropDownList runat="server" ID="dataServiceTypeId" Enabled="false" DataSourceID="ServiceTypeIdServiceTypesDataSource"
DataTextField="ServiceCode" DataValueField="ServiceTypeId" SelectedValue='<%# Bind("ServiceTypeID") %>'
AppendNullItem="true" Required="true" NullItemText="< Please Choose ...>" ErrorText="Required" />
<data:ServiceTypesDataSource ID="ServiceTypeIdServiceTypesDataSource" runat="server"
SelectMethod="GetAll" />
</ItemTemplate>
<EditItemTemplate>
<data:EntityDropDownList runat="server" ID="dataServiceTypeId" DataSourceID="ServiceTypeIdServiceTypesDataSource" OnSelectedIndexChanged="Service_SelectChange" AutoPostBack="true"
DataTextField="ServiceCode" DataValueField="ServiceTypeId" SelectedValue='<%# Bind("ServiceTypeID") %>'
AppendNullItem="true" Required="true" NullItemText="< Please Choose ...>" ErrorText="Required" />
<data:ServiceTypesDataSource ID="ServiceTypeIdServiceTypesDataSource" runat="server"
SelectMethod="GetAll" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Default">
<ItemTemplate>
<asp:CheckBox ID="chkDefault" Checked='<%# Bind("IsDefault") %>' Enabled="false" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkDefault" Checked='<%# Bind("IsDefault") %>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plan">
<ItemTemplate>
<data:EntityDropDownList runat="server" ID="dataPricePlanId" Width="200px" />
</ItemTemplate>
<EditItemTemplate>
<data:EntityDropDownList runat="server" ID="dataPricePlanId" Width="200px" AppendNullItem="true" NullItemText="< Please Choose ...>" />
</EditItemTemplate>
</asp:TemplateField>
And my code is this
protected void Service_SelectChange(object sender, EventArgs e)
{
dataPricePlanId.Items.Clear();
string sValue = ((EntityDropDownList)sender).SelectedValue;
int ServiceTypeId = Convert.ToInt32(sValue);
TList<PricePlans> _pricePlans = _pricePlanService.GetByServiceTypeId(ServiceTypeId);
_pricePlanService.DeepLoad(_pricePlans);
if (_pricePlans.Count > 0)
{
dataPricePlanId.DataSource = _pricePlans;
dataPricePlanId.DataTextField = "Description";
dataPricePlanId.DataValueField = "PricePlanId";
dataPricePlanId.DataBind();
}
else
{
dataPricePlanId.Items.Insert(0, new ListItem("No Plan For this Service", "0"));
}
}
i get value but my grid is not binding in second dropdownlist. Can you please help me .
Try to do it in itemdatabound event
first find drowdown using e.item.findcontrol("dropdownname").
then bind it with datasource and then set selected value.
thanks