Below is code of a GirdView in which i've taken a PagerTemplate which is working fine except the attribute PagerSettings-PageButtonCount="3".
I wanted to display only three page numbers at a time which is normally accomplished by the PageButtonCount attribute.
Asp.Net
<asp:GridView
id="grdMovies"
DataSourceID="srcMovies"
AllowPaging="true"
PageSize="3"
Runat="server"
PagerSettings-PageButtonCount="3"
OnDataBound="grdMovies_DataBound">
<PagerTemplate>
<table>
<tr>
<td>
<asp:LinkButton
id="lnkPrevious"
Text="< Prev"
CommandName="Page"
CommandArgument="Prev"
ToolTip="Previous Page"
Runat="server" />
</td>
<td>
<asp:Menu
id="menuPager"
Orientation="Horizontal"
OnMenuItemClick="menuPager_MenuItemClick"
CssClass="menu"
Runat="server" />
</td>
<td>
<asp:LinkButton
id="lnkNext"
Text="Next >"
CommandName="Page"
CommandArgument="Next"
ToolTip="Next Page"
Runat="server" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
C# Code
<script runat="server">
protected void grdMovies_DataBound(object sender, EventArgs e)
{
Menu menuPager = (Menu)grdMovies.BottomPagerRow.FindControl("menuPager");
for (int i = 0; i < grdMovies.PageCount; i++)
{
MenuItem item = new MenuItem();
item.Text = (i + 1).ToString();
item.Value = i.ToString();
if (grdMovies.PageIndex == i)
item.Selected = true;
menuPager.Items.Add(item);
}
}
protected void menuPager_MenuItemClick(object sender, MenuEventArgs e)
{
grdMovies.PageIndex = Int32.Parse(e.Item.Value);
}
</script>
How can i achieve this.
Thanks..
I guess you need to add this property in your gridview
PagerSettings-Mode="NumericFirstLast"
Related
I have a method that generates accordionpanes and panels and inside each accordionpane I create a button. This methode I use it in the listviewselectedindexchanged method to create the order object and fillup the body panel
protected void PanelCreator(Order order, List<Panel> pnllist)
{
Panel panelHead = new Panel();
panelHead.ID = "pH" + order.product;
panelHead.CssClass = "cpHeader";
//Add Label inside header panel to display text
Label lblHead = new Label();
lblHead.ID = order.product;
lblHead.Text = order.productName + " €" + order.priceValue;
panelHead.Controls.Add(lblHead);
//Create Body Panel
Panel panelBody = new Panel();
panelBody.ID = "pB" + order.product;
panelBody.CssClass = "cpBody";
AccordionPane ap = new AccordionPane();
foreach (Panel p in pnllist)
{
panelBody.Controls.Add(p);
}
Button btn = new Button();
btn.ID = "btn" + order.product;
btn.Text = "Toevoegen";
btn.Click += new EventHandler(btn_Click);
panelHead.Controls.Add(btn);
ap.ID = "ap" + order.product;
ap.HeaderContainer.Controls.Add(panelHead);
ap.ContentContainer.Controls.Add(panelBody);
accMenu.Panes.Add(ap);
}
I am trying to reach each buttons click event but don't know how to do it.
I have this method as for the click event to test a label inside the updatepanel but not working
protected void btn_Click(object sender, EventArgs e)
{
nameLabel.Text = "testinf";
}
this is my aspx page:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Panel ID="Panel1" runat="server"></asp:Panel>
<asp:Label ID="nameLabel" runat="server" Text="aa" />
<div style="overflow-x: auto;">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="main_product_id" DataSourceID="odsMainProduct" OnSelectedIndexChanged="ListView1_SelectedIndexChanged">
<ItemTemplate>
<stackpanel orientation="Horizontal" />
<td>
<asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" Font-Overline="false" Font-Bold="true" Font-Size="15px" Height="30px" Text='<%# Eval("name") %>' />
<%--<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />--%></td>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="color: white; text-align: left; width: auto">
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<td>
<asp:LinkButton ID="lnkSelect0" runat="server" CommandName="Select" Font-Overline="false" Font-Size="20px" ForeColor="red" Height="30px" Text='<%# Eval("name") %>' />
<%--<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />--%></td>
</SelectedItemTemplate>
</asp:ListView>
</div>
<asp:ObjectDataSource ID="odsMainProduct" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetMainProducts" TypeName="MainProductBLL"></asp:ObjectDataSource>
<asp:Accordion ID="accMenu" runat="server"></asp:Accordion>
</ContentTemplate>
</asp:UpdatePanel>
You need to store your button creation data in ViewState for creating these controls in page-load event, after that button click will be processed correct.
Order class should be marked as Serializable
public Order SelectedOrder
{
get
{
return ViewState["StoredOrder"] == null ? (Order)ViewState["StoredOrder"] : null;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (SelectedOrder != null)
{
PanelCreator(SelectedOrder);
}
}
protected void listviewselectedindexchanged(object sender, System.EventArgs e)
{
// I am not sure how you got order in this event, you should use your version of code, but idea is the same
ViewState["StoredOrder"] = sender as Order;
PanelCreator(SelectedOrder);
}
Actually i am trying to redirect command argument of a button present in a data list to another page. I am using Request.QueryString method to access the command argument on another page with the help of command name of the button. Please help me with it...
this is code of button present inside Data List
<asp:Button ID="Button1" runat="server" Text="Read" CommandArgument='<%# Eval("id")%>' OnClick="Button1_Click" CommandName="content"/>
this is code present in DataList Item command function
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
Response.Redirect("content.aspx?content=" +e.CommandArgument.ToString());
}
this is the onclick function code
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("content.aspx");
}
this is the code on another page(content.aspx)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String id = Request.QueryString["content"];
Label1.Text = id;
}
}
this is entire datalist code
<asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource1" Height="657px" RepeatColumns="4" RepeatDirection="Horizontal" Width="1248px" OnItemCommand="DataList1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<ItemStyle ForeColor="#000066" />
<ItemTemplate>
<table class="auto-style2">
<tr>
<td style="text-align: center">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Id") %>' Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:Image ID="Image2" runat="server" Height="250px" ImageUrl='<%# Eval("image") %>' Width="250px" />
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<br />
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="addtofav" CommandArgument='<%# Eval("id")%>' Height="30px" Width="20px" />
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:Button ID="Button1" runat="server" Text="Read" CommandArgument='<%# Eval("id")%>' OnClick="Button1_Click" CommandName="content"/>
</td>
</tr>
</table
<br />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
it does redirect to another page(content.aspx) but the label does not show the querystring text.
Try updating the DataList1_ItemCommand event to this:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "content")
{
Response.Redirect("content.aspx?content=" +e.CommandArgument.ToString());
}
}
also make sure you are checking IsPostBack in Page_Load method of this page code behind.
Yes i got the desired output. Actually i was also using another button to redirect to a diiferent page in DataList1_ItemCommand function. So i just needed to seperate the Response.redirects of the 2 buttons by putting them in if loop.
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "content")
{
Response.Redirect("content.aspx?content="+e.CommandArgument.ToString());
}
if (e.CommandName == "addtofav")
{
Response.Redirect("sortbyAZ.aspx?addtofav=" + e.CommandArgument.ToString());
}
}
Thanks You everybody for helping me out with this one
I think you are not casting the Request.QueryString["content"] to string format, try this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String id = Request.QueryString["content"];
Label1.Text = id;
}
}
You can find all information you need in this tutorial. from #microsoft msdn
Best of luck
<%# Page Language="C#" AutoEventWireup="True" %>
<%# Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>DataList Select Example</title>
<script runat="server">
ICollection CreateDataSource()
{
// Create sample data for the DataList control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("Item", typeof(Int32)));
dt.Columns.Add(new DataColumn("Qty", typeof(Int32)));
dt.Columns.Add(new DataColumn("Price", typeof(double)));
// Populate the table with sample values.
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = i * 2;
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsList.DataSource = CreateDataSource();
ItemsList.DataBind();
}
}
void Item_Command(Object sender, DataListCommandEventArgs e)
{
// Set the SelectedIndex property to select an item in the DataList.
ItemsList.SelectedIndex = e.Item.ItemIndex;
// Rebind the data source to the DataList to refresh the control.
ItemsList.DataSource = CreateDataSource();
ItemsList.DataBind();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>DataList Select Example</h3>
Click <b>Select</b> to select an item.
<br /><br />
<asp:DataList id="ItemsList"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
OnItemCommand="Item_Command"
runat="server">
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>
<SelectedItemStyle BackColor="Yellow">
</SelectedItemStyle>
<HeaderTemplate>
Items
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id="SelectButton"
Text="Select"
CommandName="Select"
runat="server"/>
Item <%# DataBinder.Eval(Container.DataItem, "Item") %>
</ItemTemplate>
<SelectedItemTemplate>
Item:
<asp:Label id="ItemLabel"
Text='<%# DataBinder.Eval(Container.DataItem, "Item") %>'
runat="server"/>
<br />
Quantity:
<asp:Label id="QtyLabel"
Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>'
runat="server"/>
<br />
Price:
<asp:Label id="PriceLabel"
Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")
%>'
runat="server"/>
</SelectedItemTemplate>
</asp:DataList>
</form>
</body>
</html>
By the way I think you don't need define Button Click event. Already defined path with variable. Just convert button to Link Button and remove Button Click Event
I am trying to have the clicked on(selected) ListView item be highlighted. However what is currently happening is the last selected item is being highlighted instead.
Here is my asp.net code:
<asp:ListView ID="UsersListView" AutoPostBack="true" runat="server" OnSelectedIndexChanging="UsersListView_SelectedIndexChanging" OnSelectedIndexChanged="UsersListView_SelectedIndexChanged" >
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr>
<td id="itemPlaceholder" runat="server">
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:LinkButton ID="UserNameLinkButton" CommandName="Select" runat="server" Text='<%# Container.DataItem %>'/>
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color: #336699;">
<td>
<asp:LinkButton ID="UserNameLinkButton" CommandName="Select" runat="server" Text='<%# Container.DataItem %>' BackgroundColor="#336699" ForeColor="White" />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
Here is my C# code beside:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
List<string> users = new List<string>()
{
"Gary",
"Joe",
"Brian"
};
UsersListView.DataSource = users;
UsersListView.DataBind();
}
}
protected void UsersListView_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
UsersListView.SelectedIndex = e.NewSelectedIndex;
}
protected void UsersListView_SelectedIndexChanged(object sender, EventArgs e)
{
}
Thank you for your help.
You have to use OnCommand for link button or use ListViewItemCommand and pass Item.DisplayIndex as argument to linkButton attribute CommandArgument while you are selecting item and in your case when link button is not to highlight because you are focusing on selecting not over LinkButton.So do your code on Server Side for HighLight Item Button.
Aspx
<asp:ListView ID="UsersListView" AutoPostBack="true" runat="server" OnSelectedIndexChanged="UsersListView_SelectedIndexChanged" OnItemCommand="UsersListView_ItemCommand" OnSelectedIndexChanging="UsersListView_SelectedIndexChanging">
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr>
<td id="itemPlaceholder" runat="server">
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:LinkButton ID="UserNameLinkButton" CommandName="Select" OnCommand="UserNameLinkButton_Command" CommandArgument="<%# Container.DisplayIndex %>" runat="server" Text='<%# Container.DataItem %>'/>
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server">
<td>
<asp:LinkButton ID="UserNameLinkButton" CommandName="Select" CommandArgument="<%# Container.DisplayIndex %>" runat="server" Text='<%# Container.DataItem %>' ForeColor="White" />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
cs(Code)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> users = new List<string>()
{
"Gary",
"Joe",
"Brian"
};
UsersListView.DataSource = users;
UsersListView.DataBind();
}
}
int selectedIndex=0;
protected void UsersListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
foreach(var item in UsersListView.Items)
{
LinkButton reset=item.FindControl("UserNameLinkButton") as LinkButton;
reset.BackColor = System.Drawing.Color.White;
reset.ForeColor = System.Drawing.Color.Blue;
}
LinkButton linkButton = (e.Item.FindControl("UserNameLinkButton")) as LinkButton;
linkButton.BackColor = System.Drawing.ColorTranslator.FromHtml("#336699");
linkButton.ForeColor = System.Drawing.Color.White;
}
protected void UsersListView_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
}
protected void UsersListView_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void UserNameLinkButton_Command(object sender, CommandEventArgs e)
{
//you can also do work here for when one of link button give command.(clicked)
}
Tested
I'm new to aspnet programming and need some help. I've been scouring the internet and can't find a solution to my problem. I basically need a way to have a textbox show only when it's corresponding checkbox is checked. The textboxes and checkboxes are part of an inner datalist; the controls have to be databound. I've tried JQuery and couldn't get it to work, so now I'm trying AJAX with the UpdatePanel--still not working right. Below is my current code which resides in a user control. I basically included code that pertains to the checkboxes/textboxes. The script manager is referenced in the master page. Any help would be appreciated.
Thanks in advance!
HTML ----->
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DataList ID="outerDataList" runat="server" OnItemDataBound="outerRep_ItemDataBound">
<ItemTemplate>
<div id="div1" runat="server" visible='<%# ((string)Eval("Code")) != "BABRB" %>'>
<asp:Label ID="lblBABProdType" Font-Size="Medium" Font-Bold="true" runat="server" Text='<%# Eval("Name") %>' style="padding-left:20px" />
<asp:DataList ID="innerDataList" runat="server" RepeatColumns="3" DataKeyField="ProductID" >
<ItemTemplate>
<div id="div4" runat="server" visible='<%# ((string)Eval("ProductCode")) == "BABBE" %>'>
<table ID="table4" runat="server" align="left" width="80%" style="margin-left:30px; font-size:smaller">
<tr>
<td width="3%">
<asp:CheckBox ID="bevCheckBox" runat="server" AutoPostBack="true"
OnCheckedChanged="chkBox_CheckedChanged"/>
</td>
<td align="left" width="7%">
<asp:ImageButton ID="bevImgBtn" runat="server" width="40" height="40" align="middle"
ImageUrl='<%# "~/ProductImages/"+Eval("Image1FileName").ToString() %>'
OnClick="ImgBtn_Click" CommandArgument='<%# Eval("ProductID") %>'/>
</td>
<td valign="middle" width="70%">
<span class="ProductName">
<asp:LinkButton ID="bevLnkBtn" runat="server" OnClick="LnkBtn_Click" CssClass="BABProductName"
CommandArgument='<%# Eval("ProductID") %>'>
<%# DataBinder.Eval(Container.DataItem, "Name")%>
<br /></asp:LinkButton>
</span>
<span class="ProductPrice">
<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")%>
</span
<asp:TextBox ID="bevQtyTxtBox" runat="server" Text="Qty: " Font-Size="XX-Small"
Width="40px" ViewStateMode="Disabled"/>
</td>
</tr>
</table>
</div>
<div id="div5" runat="server" visible='<%# ((string)Eval("ProductCode")) == "BABFO" %>' >
<table ID="table5" runat="server" align="left" width="80%" style="margin-left:30px; font-size:smaller">
<tr>
<td width="3%">
<asp:CheckBox ID="foodCheckBox" runat="server" AutoPostBack="true"
OnCheckedChanged="chkBox_CheckedChanged"/>
</td>
<td align="left" width="7%">
<asp:ImageButton ID="foodImgBtn" runat="server" width="40" height="40" align="middle"
ImageUrl='<%# "~/ProductImages/"+Eval("Image1FileName").ToString() %>'
OnClick="ImgBtn_Click" CommandArgument='<%# Eval("ProductID") %>'/>
</td>
<td valign="middle" width="70%">
<span class="ProductName">
<asp:LinkButton ID="foodLnkBtn" runat="server" OnClick="LnkBtn_Click" CssClass="BABProductName"
CommandArgument='<%# Eval("ProductID") %>'>
<%# DataBinder.Eval(Container.DataItem, "Name")%>
<br /></asp:LinkButton>
</span>
<span class="ProductPrice">
<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")%>
</span>
<asp:TextBox ID="foodQtyTxtBox" runat="server" Text="Qty: " Font-Size="XX-Small"
Width="40px" ViewStateMode="Disabled"/>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
</div>
<br />
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
CODE BEHIND ----->
protected void Page_Load(object sender, EventArgs e)
{
PopulateData();
}
private void PopulateData()
{
// Retrieve list of products
string categoryID = Request.QueryString["BABCategoryID"];
// set the stored procedure name
SqlConnection sqlConn = new SqlConnection(ChocolateExpConfiguration.DbConnectionString);
SqlCommand sqlComm = new SqlCommand("GetBABProductsInCategory", sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adptr = new SqlDataAdapter(sqlComm);
sqlComm.Parameters.AddWithValue("#BABCategoryID", categoryID);
DataSet ds = new DataSet();
adptr.Fill(ds);
ds.Relations.Add(new DataRelation("CodeRelation", ds.Tables[0].Columns["Code"], ds.Tables[1].Columns["ProductCode"]));
outerDataList.DataSource = ds.Tables[0];
outerDataList.DataBind();
}
protected void outerRep_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView drv = e.Item.DataItem as DataRowView;
DataList innerDataList = (DataList)e.Item.FindControl("innerDataList");
innerDataList.DataSource = drv.CreateChildView("CodeRelation");
innerDataList.DataBind();
}
}
protected void chkBox_CheckedChanged(object sender, EventArgs e)
{
foreach (DataListItem parentItem in outerDataList.Items)
{
//Find nested items(DataList) of parent Datalist
DataList innerDataList = (DataList)parentItem.FindControl("innerDataList");
foreach (DataListItem childItem in innerDataList.Items)
{
CheckBox bevCheckBox = (CheckBox)childItem.FindControl("bevCheckBox");
CheckBox foodCheckBox = (CheckBox)childItem.FindControl("foodCheckBox");
TextBox bevQtyTxtBox = (TextBox)childItem.FindControl("bevQtyTxtBox");
TextBox foodQtyTxtBox = (TextBox)childItem.FindControl("foodQtyTxtBox");
if ((bevCheckBox.Checked == true) || (foodCheckBox.Checked == true))
{
bevQtyTxtBox.Visible = true;
foodQtyTxtBox.Visible = true;
}
else
{
bevQtyTxtBox.Visible = false;
foodQtyTxtBox.Visible = false;
}
UpdatePanel1.Update();
}
}
}*
protected void EnableTextBox()
{
int count = int.Parse(GridView1.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
CheckBox cb1 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
CheckBox cb2 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox3");
TextBox tb = (TextBox)GridView1.Rows[i].Cells[4].FindControl("txtration");
TextBox tb1 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("txtjob");
TextBox tb2 = (TextBox)GridView1.Rows[i].Cells[6].FindControl("txtaadhar");
if (cb.Checked == true)
{
tb.Visible = true;
}
else
{
tb.Visible = false;
}
if (cb1.Checked == true)
{
tb1.Visible = true;
}
else
{
tb1.Visible = false;
}
if (cb2.Checked == true)
{
tb2.Visible = true;
}
else
{
tb2.Visible = false;
}
}
}
in checkedchanged event
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
EnableTextBox();
}
I have a scenario as below.
There are 3 check boxes.3 textbox associated with each.Grid gets populated on entering a search value in the textbox.On selecting a row on this grid,I need to hide the grid and selected value(one column) is populated into another textbox.I am getting "Object reference not set to an instance of an object.",when I implementing the logic for hiding the grid.Checkbox checked,value entered in search textbox,grid is populated ,a row selected,second textbox populated with grid value,grid gets hidden--> after this process repeated for 2-3 times,again checking on a check box gives the error.
Please find the markup with above controls.
<table width="500px">
<tr>
<td>
<fieldset id="fssearch" runat="server">
<legend>Search </legend>
<table>
<tr>
<td>
<asp:CheckBox ID="CBFile" runat="server" Text="File No" OnCheckedChanged="CBFile_CheckedChanged"
AutoPostBack="true" />
</td>
<td>
<asp:CheckBox ID="CBname" runat="server" Text="Patient Name" OnCheckedChanged="CBname_CheckedChanged"
AutoPostBack="true" />
</td>
<td>
<asp:CheckBox ID="CBMobile" runat="server" Text="Mobile No" OnCheckedChanged="CBMobile_CheckedChanged"
AutoPostBack="true" />
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="RTFileS" onkeyup="KeyUp();" runat="server" OnTextChanged="RTFileS_TextChanged" Visible="false"></asp:TextBox>
</td>
<td colspan="2">
<asp:TextBox ID="RTNameS" onkeyup="KeyUp();" runat="server" OnTextChanged="RTNameS_TextChanged" Visible="false"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="RTMobileS" onkeyup="KeyUp();" runat="server" OnTextChanged="RTMobileS_TextChanged" Visible="false"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="4">
<telerik:RadGrid ID="gvPatientList" runat="server" AllowFilteringByColumn="True"
AllowPaging="True" GridLines="None" OnSelectedIndexChanged="gvPatientList_SelectedIndexChanged" >
<PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
<GroupingSettings CaseSensitive="false" />
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<AlternatingItemStyle HorizontalAlign="Left" />
<ClientSettings EnablePostBackOnRowClick="true">
<Selecting AllowRowSelect="true" />
</ClientSettings>
<MasterTableView AutoGenerateColumns="False" DataKeyNames="pt_regid">
<CommandItemTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</CommandItemTemplate>
<Columns>
<telerik:GridBoundColumn HeaderText="Patient Name" UniqueName="pt_name" DataField="pt_name"
AllowFiltering="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="File No" UniqueName="pt_fileno" DataField="pt_fileno"
AllowFiltering="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Mobile" UniqueName="pt_pmobileno" DataField="pt_pmobileno"
AllowFiltering="false">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
<HeaderContextMenu EnableImageSprites="True" CssClass="GridContextMenu GridContextMenu_Default">
</HeaderContextMenu>
</telerik:RadGrid>
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
</table>
On selecting a row on the grid,I am populating name column value to a textbox and hiding the grid as below.
protected void gvPatientList_SelectedIndexChanged(object sender, EventArgs e)
{
GridDataItem RegId = gvPatientList.SelectedItems[0] as GridDataItem;
string regid = RegId.GetDataKeyValue("pt_regid").ToString();
foreach (GridDataItem dataItem in gvPatientList.Items)
{
if (dataItem.Selected)
{
RCFName.Text = dataItem["pt_name"].Text;
}
}
gvPatientList.Visible = false;
}
Above mentioned checkboxes act like radio buttons and javascript for the same is as below
<script language="CS" runat="server">
private void makeRadioGroupFromCheckBoxes(IEnumerable<CheckBox> checkBoxes)
{
StringBuilder sb = new StringBuilder();
foreach (CheckBox cb in checkBoxes)
{
foreach (CheckBox innercb in checkBoxes)
{
if (innercb != cb)
{
sb.Append("document.getElementById('");
sb.Append(innercb.ClientID);
sb.Append("').checked = false;");
}
}
cb.Attributes["onclick"] = "if(this.checked){" + sb.ToString() + "}else{this.checked = true;}";
sb = new StringBuilder();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.makeRadioGroupFromCheckBoxes(new CheckBox[] { CBFile, CBname, CBMobile });
}
}
Grid gets populated on textchanged event and 'onkeyup="KeyUp();' function of the textbox,associated with it as below.
aspx:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
var timer = null;
function KeyUp() {
if (timer != null) {
clearTimeout(timer);
}
timer = setTimeout(LoadTable, 500);
}
function LoadTable() {
$find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("FilterGrid");
}
</script>
</telerik:RadCodeBlock>
code behind:
protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
{
if (e.Argument.IndexOf("FilterGrid") != -1)
{
gvPatientList.Rebind();
}
}
protected void RTFileS_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(RTFileS.Text))
{
_scheduleService = new ScheduleService();
clsSchedule clsschedule = new clsSchedule();
string search = " where OldRegnNo like '" + RTFileS.Text + "%" + "'";
gvPatientList.DataSource = _scheduleService.GetAllPatients(search);
gvPatientList.Rebind();
}
}
On checkedchanged eveent of check boxes,I am making the grid visible.
protected void CBFile_CheckedChanged(object sender, EventArgs e)
{
if (CBFile.Checked)
{
RTFileS.Visible = true;
gvPatientList.Visible = true;
gvPatientList.MasterTableView.Visible = true;
gvPatientList.Rebind();
}
else
{
RTFileS.Visible = false;
}
}
Ajaxmanager is as below:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="gvPatientList">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="gvPatientList" LoadingPanelID="RadAjaxLoadingPanel1" />
<telerik:AjaxUpdatedControl ControlID="RCFName" />
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="gvPatientList" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
I hope hiding/unhiding the grid gives me the error.Please suggest your ideas on this.
Thanks,
Soumya
Please try with below code snippet. You get error because you try to ajaxify grid which was visible false.
protected void Page_PreRender(object sender, EventArgs e)
{
if (gvPatientList.Visible)
{
RadAjaxManager1.AjaxSettings.AddAjaxSetting(gvPatientList, gvPatientList, RadAjaxLoadingPanel1);
RadAjaxManager1.AjaxSettings.AddAjaxSetting(gvPatientList, RCFName);
}
}