How to update an editItemTemplate checkboxlist from codebehind? - asp.net

I am using a gridview fed from a SQL database. I need to add a custom itemTemplate that will consist of a checkboxlist fed from another datasource. Here is my xml part :
<asp:TemplateField HeaderText="Equipements" >
<EditItemTemplate>
<asp:CheckBoxList ID="myCB" runat="server">
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
I am using the gridview onEditing event to try to access "myCB" in the code behind :
protected void OnEditing(object sender, EventArgs e)
{
GridView gridview = sender as GridView;
GridViewEditEventArgs editEvent = e as GridViewEditEventArgs;
ListViewItemEventArgs rowEvent = e as ListViewItemEventArgs;
TableCell equipementsCell = gridview.Rows[editEvent.NewEditIndex].Cells[11];
CheckBoxList equipements = gridview.Rows.FindControl("myCB") as CheckBoxList;
}
This code doesn't work, the checkboxlist isn't found. I have tried many things unsuccessfully...

You should use GridView's RowDataBound event:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow
&& e.Row.RowState == DataControlRowState.Edit)
{
CheckBoxList equipements = (CheckBoxList )e.Row.FindControl("myCB");
equipements.DataSource = getSomeData();
equipements.DataTextField = "TextColumn";
equipements.DataTextField = "IdColumn";
equipements.DataBind();
}
}

Related

Set data to a label in GridView

I have a label in an asp GridView and I want to set bind data to it.
Here is my RowDataBound method:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
var name = (Label)e.Row.FindControl("Label_name");
name.Text ="sara";
name.DataBind();
}
and here is my GridView:
<asp:GridView ID="GridViewCertificateType" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" >
<asp:Label ID="Label_name" runat="server" Text="" ></asp:Label>
But after name.Text ="sara";, I receive this exception:
Object reference not set to an instance of an object.
In your RowDataBound method, make sure you have a condition to check what the type of the row is. This event will be called for every row in your GridView, including header and footer rows. If Label_name is not found in the header row, you'll have a null object. Once you do that, get rid of name.DataBind();, as it is not needed.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var name = (Label)e.Row.FindControl("Label_name");
name.Text = "sara";
}
}
try this
var name= (Label)e.Row.Cells[X].FindControl("Label_name");
name.Text ="sara";
name.DataBind();
X means index of column

Unable to do paging on gridview asp.net,

I have code to have a dynamic button appear after it does an if else loop for checking for some data.
however, the gridview cant do any paging. it only shows the first page and cant go to 2nd page and so on.
It only can do the paging IF i nvr call the method below.
here is my code
public void filter_select(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
........
}
that method will be called on the rowdatabound gridview page source like this OnRowDataBound="filter_select"
my gridview code on source code is :
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="2" CellSpacing="2"
HorizontalAlign="Center" OnRowCommand="stop_survey"
OnRowDataBound="filter_select" OnSelectedIndexChanging="selected" PageSize="5"
Width="133%" DataKeyNames="SurveyID" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
>
<Columns>
<asp:BoundField DataField="SurveyID" HeaderText="Survey ID" ReadOnly="True"
.........
my onrowCommand code
public void stop_survey(object sender, GridViewCommandEventArgs e)
{
SqlConnection con;
string sqlcom;
SqlCommand cmd;
//is it because of this code below? as if i comment from this code onwards, the paging works.
GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
Button btn = (Button)GridView1.Rows[row.RowIndex].FindControl("btnStop");
...............
Rebind your gridview in the PageIndexChanging event of the gridview control
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = DataAcceessLayer.GetEmployeeData();
GridView1.DataBind();
}

Acessing Dropdownlist(selected value) in gridview

I have a bit of trouble with getting dropdownlist selected value we have in aspx a Gridview and in it dropdownlist:
<asp:GridView ID="grid1" runat="server" autogeneratecolumns="true" >
<Columns>
<asp:BoundField HeaderText="something" />
<asp:TemplateField HeaderText="filtras">
<HeaderTemplate>
<asp:DropDownList ID="dropdown1" runat="server"
OnLoad="dropdownLoad"
OnSelectedIndexChanged="updatetable" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
We fill the DropDownList with values using OnLoad event, and then when we select something from the DropDownList, the event OnSelectedIndexChange should allow us to take the selected value and to do what we want with it (filter the grid in this case), but OnSelectedIndexChange never gets executed.
protected void Page_Load(object sender, EventArgs e)
{
//Create Gridview + fill with values
if (IsPostBack)
{
return;
}
ArrayList mycountries = new ArrayList();
mycountries.Add("Norway");
mycountries.Add("Sweden");
mycountries.Add("France");
mycountries.Add("Italy");
mycountries.TrimToSize();
mycountries.Sort();
rb.DataSource = mycountries;
rb.DataBind();
grid1.DataSource = mycountries;
grid1.DataBind();
}
protected void dropdownLoad(object sender, EventArgs e)
{ // fill dropDownList in GridView with data
DropDownList dropdown = sender as DropDownList;
if (dropdown != null)
{
ArrayList mycountries = new ArrayList();
mycountries.Add("Norway");
mycountries.Add("Sweden");
mycountries.Add("France");
mycountries.Add("Italy");
mycountries.TrimToSize();
mycountries.Sort();
dropdown.DataSource = mycountries;
dropdown.DataBind();
TextBox1.Text = dropdown.SelectedIndex.ToString();
}
}
protected void updatetable(object sender, EventArgs e)
{// after dropDownList element was selected change dropdownlist values/or filter the table...
//this part is never executed ! Why?
DropDownList dropdown = sender as DropDownList;
if (dropdown != null)
{
ArrayList mycountries = new ArrayList();
mycountries.Add("UK");
mycountries.Add("USA");
mycountries.Add("Sweden");
mycountries.Add("Hungary");
mycountries.TrimToSize();
mycountries.Sort();
dropdown.DataSource = mycountries;
dropdown.DataBind();
}
}
How can I get the DropDownList's selected value? My debugger shows that OnSelectedIndexChange is never executed.
I tried following the suggestions in this question Setting selectedvalue for a dropdownlist in GridView, but that didn't work.
Have you tried setting AutoPostBack to true for your dropdown control so that it causes a postback?

How to change the GridView page Index

How to change Gridview pages based on the dropdown list selected index? the dropdown list is not inside the gridview, any help would be appreciated.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.PageIndex = DropDownList1.SelectedItem.Value;
GridView1.DataSource = datatable;
GridView1.DataBind();
}
What you need to do is set the GridView's PageSize property to the value of your DropDownList's selected item value:
private void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
gridview.PageSize = DropDownListPageSize.SelectedValue;
}

Why is the footer-item not included in Repeater.Items?

I need to get a value from a textbox inside a FooterTemplate in the OnClick event of a button. My first thought was to loop through the items-property on my repeater, but as you can see in this sample, it only includes the actual databound items, not the footer-item.
ASPX:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
Item<br />
</ItemTemplate>
<FooterTemplate>
Footer<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Code-behind.cs:
protected void Page_Load(object sender, EventArgs e)
{
ListItemCollection items = new ListItemCollection();
items.Add("value1");
items.Add("value2");
Repeater1.DataSource = items;
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(Repeater1.Items.Count);
}
This code will only output "2" as the count, so how do I get to reference my textbox inside the footertemplate?
From the MSDN documentation, the Items is simply a set of RepeaterItems based off the DataSource that you are binding to and does not include items in the Header or FooterTemplates.
If you want to reference the textbox, you can get a reference on ItemDataBound event from the repeater where you can test for the footer.
E.g.
private void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox textBox = e.Item.FindControl("TextBox1") as TextBox;
}
}
You can find controls in the repeater. That will give you all the controls in the repeater (RepeaterItems collection). Now you can do something like this:
RepeaterItem footerItem=null;
foreach(Control cnt in Repeater1.Controls)
{
if(cnt.GetType() == typeof(RepeaterItem) && ((RepeaterItem)cnt).ItemType == ListItemType.Footer)
{
footerItem = cnt;
break;
}
}
The footer should be the last child control of the repeater so you can do something like..
RepeaterItem riFooter = Repeater1.Controls[Repeater1.Controls.Count - 1] as RepeaterItem;
if (riFooter != null && riFooter.ItemType == ListItemType.Footer) {
TextBox TextBox1 = riFooter.FindControl("TextBox1") as TextBox;
if (TextBox1 != null) {
TextBox1.Text = "Test";
}
}

Resources