Getting textbox's value in Nested gridview - c#

I have a nested gridview, And there is a textbox that named TextBoxDescription to insert user information in the second gridview. But when I want to catch textbox's (TextBoxDescription) value its return Null.
Html Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
DataKeyNames="ReportId" OnRowDataBound="GridView2_OnRowDataBound" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<ItemStyle Width="35px" HorizontalAlign="Center" />
<ItemTemplate>
<img alt="" style="cursor: pointer" src="plus.png" />
<asp:Panel ID="Panel5" runat="server" Style="display: none; text-align: center;">
<asp:GridView ID="GridView2" border="0" runat="server" DataKeyNames ="ReportId" OnRowCommand="GridView1_RowCommand"
Style="direction: rtl" Width="100%" Height="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" height="100%" bgcolor="#F4F4F4">
<tr>
</tr>
<tr>
<td style="width: 50%; height: 50%" hidefocus="hidefocus" unselectable="off" valign="top">
<asp:Panel ID="Panel3" runat="server" GroupingText="یاد داشت">
<asp:TextBox ID="TextBoxDescription" runat="server" Style="resize: none; width: 612px;
height: 160px;" Text='<%# Eval("UserDescription") %>' TextMode="MultiLine"></asp:TextBox>
<br />
<br />
<asp:LinkButton ID="LinkButtonSave" CommandName ="updateData" CommandArgument='<%#Eval("ReportId") %>' runat="server">ذخیره</asp:LinkButton>
</asp:Panel>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.
.
.
C# Code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "updateData")
{
int i = Convert.ToInt32(e.CommandArgument);
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox tb = (TextBox)row.FindControl("TextBoxDescription");
string Text = tb.Text;
}
}

Do like below :
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int index = row.RowIndex;
TextBox tb= (TextBox )GridView1.Rows[index].FindControl("TextBoxDescription");

http://forums.asp.net/p/2053658/5919172.aspx?Getting+textbox+s+value+in+Nested+gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView2_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Panel ID="Panel3" runat="server" GroupingText="یاد داشت">
<asp:TextBox ID="TextBoxDescription" runat="server" Style="resize: none; width: 612px; height: 160px;"
Text='<%# Eval("UserDescription") %>' TextMode="MultiLine"></asp:TextBox>
<br />
<br />
<asp:LinkButton ID="LinkButtonSave" CommandName="updateData" CommandArgument='<%#Eval("ReportId") %>' runat="server">ذخیره</asp:LinkButton>
</asp:Panel>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable gdv1source = new DataTable();
gdv1source.Columns.Add("ID");
gdv1source.Rows.Add("1");
GridView1.DataSource = gdv1source;
GridView1.DataBind();
}
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox tb = (TextBox)row.FindControl("TextBoxDescription");
string Text = tb.Text;
lbltext.Text = "Text Value is: " + Text;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gdv2 = (GridView)e.Row.FindControl("GridView2");
BindGridView2(gdv2);
}
}
private static void BindGridView2(GridView gdv2)
{
DataTable gdv2source = new DataTable();
gdv2source.Columns.Add("UserDescription");
gdv2source.Columns.Add("ReportId");
gdv2source.Rows.Add("UserDescription1", "1");
gdv2source.Rows.Add("UserDescription2", "2");
gdv2.DataSource = gdv2source;
gdv2.DataBind();
}

Related

Get modal popup values in gridview row textbox

I have a gridview with dynamic controls. Resource name is something that I am picking up from Active directory. Search filter works in a way where you have to enter lastname, firstname and onlclick of lookup it will search. If there are more than one record a modalpopup will be displayed with dropdownlist with options to select from.
The issue is that I am unable to get the dropdown values populated into textbox present in gridview. I need this dropdown value in correct selected row's textbox.
Code behind:
protected void gvProjectResource_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Lookup")
{
GridViewRow clickedRow = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
TextBox t2 = ((TextBox)clickedRow.FindControl("t2"));
DataTable dt = globalObj.FindPersons(t2.Text.Split(',')[0].Trim(), t2.Text.Split(',')[1].Trim());
if (dt.Rows.Count > 1)
{
mpFindPerson.Show();
ddlPersons.Items.Clear();
ddlPersons.Items.Add(new ListItem { Value = "0", Text = "-Select User-" });
ddlPersons.DataSource = dt;
dt.Columns.Add("FullName", typeof(string), "DisplayName + ' | ' + MSID");
ddlPersons.DataTextField = "FullName";
ddlPersons.DataValueField = "MSID";
ddlPersons.DataBind();
}
else
{
t2.Text = dt.Rows[0].ItemArray[0].ToString();
}
}
if (e.CommandName == "Del")
{
}
}
protected void btnSelectPerson_Click(object sender, EventArgs e)
{
TextBox t2 = (gvProjectResource.SelectedRow.FindControl("t2") as TextBox);
t2.Text = ddlPersons.SelectedItem.Text;
mpFindPerson.Hide();
}
ASPX Code:
<div id="four">
<fieldset style="border: solid; border-width: thin; height: 220px; border-color: #a8a8a8;">
<legend id="Legend11" runat="server" visible="true" style="width: auto; margin-bottom: 0px; font-size: 12px; font-weight: bold; color: #1f497d;"> Project Resources </legend>
<div style="height: 210px; overflow: auto;">
<asp:GridView ID="gvProjectResource" Width="88%" HeaderStyle-Font-Size="X-Small" CssClass="labels" runat="server"
ShowFooter="true" AutoGenerateColumns="false" Style="margin-right: auto; margin-left:7px;"
OnRowDataBound="gvProjectResource_RowDataBound"
OnRowCommand="gvProjectResource_RowCommand" >
<Columns>
<asp:TemplateField HeaderText="Role" ItemStyle-VerticalAlign="Top" FooterStyle-VerticalAlign="Top" ItemStyle-Width="50%">
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text='<%# Eval("role") %>' Visible="false" />
<asp:DropDownList ID="ddlRole" Height="23px" Width="98%" runat="server" CssClass="DropDownListStyleOverview"></asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlRole2" Height="23px" Width="98%" runat="server" CssClass="DropDownListStyleOverview"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Resource Name" ItemStyle-VerticalAlign="Top" FooterStyle-VerticalAlign="Top" ItemStyle-Width="50%">
<ItemTemplate>
<asp:TextBox ID="t1" Height="23px" Width="98%" runat="server" ></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="t2" Height="23px" Width="68%" runat="server"></asp:TextBox>
<asp:LinkButton runat="server" Text="Lookup" CommandName="Lookup" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-VerticalAlign="Top" ItemStyle-Width="5%">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Delete" CommandName="Del" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<div style="text-align: right;">
<asp:LinkButton runat="server" Text="Add New" ID="LinkButton2" CssClass="labels"></asp:LinkButton>
</div>
</div>
<asp:Button runat="server" ID="btnModalPopUpSearch" Style="display: none" />
<AjaxToolkit:ModalPopupExtender ID="mpFindPerson"
BehaviorID="mpFindPersonBehav"
runat="server"
DropShadow="true"
BackgroundCssClass="modalBackground"
PopupControlID="pnlFindPerson"
TargetControlID="btnModalPopUpSearch">
</AjaxToolkit:ModalPopupExtender>
<asp:Panel runat="Server" ID="pnlFindPerson" CssClass="modalPopup" style="position:relative;">
<div class="labels" runat="server" style="text-align:center; position:absolute; top:20%; height:10em; margin-top:auto; margin-left:20px;">
<asp:DropDownList ID="ddlPersons" runat="server" AppendDataBoundItems="true" Width="200px">
<asp:ListItem Text="-Select User-" Value="0" />
</asp:DropDownList>
<br />
<br />
<br />
<div style="text-align: center;">
<asp:Button runat="server" Font-Size="9pt" Text="Select" CssClass="buttons___" Style="float: left;"
ID="btnSelectPerson" OnClick="btnSelectPerson_Click" />
</div>
</div>
</asp:Panel>
</fieldset>
</div>
ok, so, there was nothing much to do.
have to add this.
protected void btnSelectPerson_Click(object sender, EventArgs e)
{
TextBox t2 = (TextBox)gvProjectResource.FooterRow.FindControl("t2");
t2.Text = ddlPersons.SelectedItem.Text;
mpFindPerson.Hide();
}

Display nested gridview

I have a nested GridView with 4 levels,
when i click in "+" to show child GridView i make request to database to download data of current row, every thing work well for me, the only problem i have is in design, all the child GridView display in column of its parent GridView
this is how it looks:
Parent GridView
First Child gridView
here is my aspx code:
<asp:UpdatePanel ID="upNestedGridView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvCostCenters" runat="server" ....>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgShowAccountingPlan" runat="server" OnClick="Show_Hide_AccountingPlansGrid" .../>
<asp:Panel ID="pnlAccountingPlan" runat="server" Visible="false" Style="position: relative">
<asp:GridView ID="gvAccountingPlans" runat="server" AutoGenerateColumns="false"....">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgShowPrograms" runat="server" OnClick="Show_Hide_ProgramsGrid" .../>
<asp:Panel ID="pnlPrograms" runat="server" Visible="false" Style="position: relative">
<asp:GridView ID="gvPrograms" runat="server" AutoGenerateColumns="false" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgShowProjects" runat="server" OnClick="Show_Hide_ProjectsGrid" ..../>
<asp:Panel ID="pnlProjects" runat="server" Visible="false" Style="position: relative">
<asp:GridView ID="gvProject" runat="server" ....>
.....
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Label" HeaderText="البند " ItemStyle-HorizontalAlign="Right" />
....
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NumAccountingPlan" HeaderText="الخطة المحاسبية " ItemStyle-HorizontalAlign="Right" />
...
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
...
<asp:BoundField DataField="OperatingExpenses" HeaderText="المصروفات التشغيلية" DataFormatString="{0:#,##0.00;(#,##0.00);0}" />
</Columns>
</asp:GridView>
my jquery code:
<script type="text/javascript">
$(function () {
$("[src*=minus]").each(function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
$(this).next().remove()
});
});
</script>
My code C#:
protected void Show_Hide_AccountingPlansGrid(object sender, EventArgs e)
{
try
{
ServiceClass service = new ServiceClass();
ImageButton imgShowHide = (sender as ImageButton);
GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
if (imgShowHide.CommandArgument == "Show")
{
_budget = service.GetBudgetById(int.Parse(hfIdBudget.Value));
row.FindControl("pnlAccountingPlan").Visible = true;
imgShowHide.CommandArgument = "Hide";
imgShowHide.ImageUrl = "/Content/img/minus.gif";
string idCostCenter = gvCostCenters.DataKeys[row.RowIndex].Value.ToString();
GridView gvAccountingPlans = row.FindControl("gvAccountingPlans") as GridView;
//gvAccountingPlans.ToolTip = costCenterId;
gvAccountingPlans.DataSource = AccountingPlanData(int.Parse(hfIdUser.Value), int.Parse(hfIdBudget.Value), int.Parse(idCostCenter));
gvAccountingPlans.DataBind();
}
else
{
row.FindControl("pnlAccountingPlan").Visible = false;
imgShowHide.CommandArgument = "Show";
imgShowHide.ImageUrl = "/Content/img/plus.gif";
}
}
catch (Exception ex) { GlobalHelpers.Trace(ex); }
}
I notice that when i delete the UpdatePanel the first child GridView display well, but the others no.
How can i do to display all childs GridView well?
I'm sorry for my bad english
Code asp:
<asp:UpdatePanel ID="upNestedGridView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvCostCenters" runat="server" AutoGenerateColumns="false" ....>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgShowAccountingPlan" runat="server" OnClick="Show_Hide_AccountingPlansGrid" ImageUrl="/Content/img/plus.gif" CommandArgument="Show" />
</ItemTemplate>
</asp:TemplateField>
....
<asp:TemplateField>
<ItemTemplate>
<tr visible="false" runat="server" id="trAccountingPlan">
<td visible="false" runat="server" id="tdAccountingPlan2"></td>
<td colspan="999" visible="false" runat="server" id="tdAccountingPlan">
<asp:GridView ID="gvAccountingPlans" runat="server" ....>
<Columns>
.....
</Columns>
</asp:GridView>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
Code C#:
protected void Show_Hide_AccountingPlansGrid(object sender, EventArgs e)
{
try
{
ImageButton imgShowHide = (sender as ImageButton);
GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
if (imgShowHide.CommandArgument == "Show")
{
...
imgShowHide.CommandArgument = "Hide";
imgShowHide.ImageUrl = "/Content/img/minus.gif";
row.FindControl("trAccountingPlan").Visible = true;
HtmlTableCell tdAccountingPlan = (HtmlTableCell)row.FindControl("tdAccountingPlan");
HtmlTableCell tdAccountingPlan2 = (HtmlTableCell)row.FindControl("tdAccountingPlan2");
tdAccountingPlan2.Visible = true;
tdAccountingPlan.Visible = true;
...
}
else
{
imgShowHide.CommandArgument = "Show";
imgShowHide.ImageUrl = "/Content/img/plus.gif";
row.FindControl("trAccountingPlan").Visible = false;
HtmlTableCell tdAccountingPlan = (HtmlTableCell)row.FindControl("tdAccountingPlan");
HtmlTableCell tdAccountingPlan2 = (HtmlTableCell)row.FindControl("tdAccountingPlan2");
tdAccountingPlan2.Visible = false;
tdAccountingPlan.Visible = false;
}
}
catch (Exception ex) { GlobalHelpers.Trace(ex); }
}

cannot find controls in gridview and get an error(Index was out of range)

Actually I have problem with finding LinkButton Control in gridview .
I have 2 gridviews which one of them is inside another one so my problem is I cannot get the value of LinkButton of second gridview,
here is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderStyle="None" DataSourceID="SqlDataSource2" GridLines="None">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<table align="center" class="Table3">
<tr>
<td >
<asp:Label ID="lblID" runat="server" Visible="false" Text='<%# Eval("Food_ID") %>'></asp:Label>
<b> <%#Eval("Title")%></b>
</td>
</tr>
<tr>
<td align="center" class="ImgKidFood">
<asp:Image ID="Img" Width="680px" Height="145px" ImageUrl='<%#Eval("Pictures") %>' runat="server" />
</td>
</tr>
<tr>
<td style="direction:rtl; text-align:right;">
<asp:GridView ID="ShowFoodMenu2" runat="server" AutoGenerateColumns="False"
BorderStyle="None" GridLines="None" ShowHeader="False"
DataSourceID="SqlDataSource1" Width="100%"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table align="center" class="TableListMenu">
<tr>
<td class="Add">
<asp:LinkButton ID="LinkButton2" CommandArgument="<%# Container.DataItemIndex %>"
runat="server" CausesValidation="False"
CommandName="Select" Text="Select"
onclick="LinkButton2_Click" ></asp:LinkButton>
</td>
<td class="ToCenter">
<b><%#Eval("Title_Pr") %>
</b>
<asp:LinkButton ID="LinkButton12" runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Link2" Text='<%# Eval("Menu_ID") %>'></asp:LinkButton>
</td>
<td class="PriceLeft">
<%#Eval("Price") %>
</td>
</tr>
</table>
<hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><br /><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
As u can see the second gidview named id is ShowFoodMenu2 and it's inside first girdview which id Giridview1 .
and also I have two Linkbuttons inside the second gridview
one of them keep the value(which is LinkButton12) and another one(LinkButton2) is for when I clicked it add a record in Database .
But when I clicked on Linkbutton(LinkButton2) to show the value of Linkbutton12 , I will get ann error
Here is the error
http://hidelion.com/Images/error.png
and here is my.cs code
protected void LinkButton2_Click(object sender, EventArgs e)
{
GridView G = new GridView();
G.FindControl("ShowFoodMenu2");
System.Threading.Thread.Sleep(1000);
LinkButton m = (LinkButton)sender;
int i = Int32.Parse(m.CommandArgument);
LinkButton LblMososID = (LinkButton)G.Rows[i].FindControl("LinkButton2");
// LinkButton LblMososID2 = (LinkButton)G.Rows[i].FindControl("LinkButton12");
Label1.Text = LblMososID.Text;
}
So how can I solved this problem ??????
GridView G = new GridView(); just create a new instance of Gridview class, and it will not find your gridview automatically, You are messing it up, simply go like this, from linkbutton find the corresponding row, from that row fetch the control you wish to manipulate, do it like this:-
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton LinkButton2 = sender as LinkButton;
GridViewRow grdRow = (GridViewRow)LinkButton2.NamingContainer;
LinkButton LinkButton12 = (LinkButton)grdRow.FindControl("LinkButton12 ");
Label1.Text = LinkButton12.Text;
}

PostBack from a link in Grid within UpdatePanel

I have a grid and few textboxes within an UpdatePanel. On selecting a paricular row the textboxes are filled using JS. UpdatePanel was introduced as one textbox was filled using a method in the code.
There is also a link in grid row which opens a pop-up(also filled from code behind).
After using UpdatePanel the link is not working(assuming because of no full post back).
I tried using UpdateMode="Conditional" property and registering Link control for PostBAck control.
But still it is not working:
Code
<asp:UpdatePanel ID="Update" runat="server" Up>
<ContentTemplate>
<table width="100%">
<tr>
<td>
<asp:GridView ID="gvSession" BorderColor="#ffcc00" RowStyle-BorderColor="#ffcc00"
AutoGenerateColumns="False" Font-Names="Verdana" DataKeyNames="SessionId" runat="server"
RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1px" GridLines="Both" Width="100%"
OnRowCreated="gvSession_RowCreated" OnDataBound="gvSession_DataBound">
<RowStyle CssClass="dbGrid_Table_row" />
<HeaderStyle CssClass="dbGrid_Table_Header" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rbSelect" onclick="javascript:CheckOtherIsCheckedByGVIDMore(this);"
runat="server" OnCheckedChanged="rbSelect_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Session Name">
<ItemTemplate>
<asp:Label ID="lblSessionName" Text='<%# Eval("SessionName") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="16%"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Preview">
<ItemTemplate>
<asp:LinkButton ID="lbPreview" Text="Preview" CommandName="Preview" AutoPostBack="true"
CommandArgument='<%# Eval("SessionId") + ";" + Eval("TrainingTypeName") %>'
runat="server" OnClick="lbPreview_Click"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
<table id="Table2" runat="server" width="100%">
<tr>
<td colspan="2" align="center" style="text-align: center">
<asp:TextBox ID="txtSessionDtl" runat="server" Font-Size="Small" Style="text-align: center"
ForeColor="Black" Wrap="true" Height="20px" Width="95%" BorderStyle="None"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 12%">
<asp:Label ID="Label9" CssClass="label" runat="server">Upload File : </asp:Label>
</td>
<td>
<asp:TextBox ID="txtFilePath" Enabled="false" BorderStyle="Solid" BorderColor="Black"
runat="server" Width="741px"></asp:TextBox>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
FOr Link click:
protected void lbPreview_Click(object sender, EventArgs e)
{
try
{
LinkButton link = (LinkButton)sender;
GridViewRow gv = (GridViewRow)(link.Parent.Parent);
LinkButton linkPreview = (LinkButton)gv.FindControl("lbPreview");
string[] arg = new string[2];
arg = linkPreview.CommandArgument.ToString().Split(';');
string strSessionId = arg[0];
string strTrgType = arg[1];
string mailContent = GenerateNominationMailer(strSessionId, strTrgType);
hidMailContent.Value = mailContent;
string mailContent1 = mailContent.Replace(System.Environment.NewLine, "");
string myScript = "<SCRIPT LANGUAGE='javascript'>";
myScript = myScript + " my_window = window.open('', '', 'status=1,width=1000,height=800,scrollbars=yes');";
myScript = myScript + " my_window.document.write(\"" + mailContent1.Replace("\"", "'") + "\");";
myScript = myScript + " my_window.document.close();";
myScript = myScript + " </script>";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Nomination Mailer", myScript,false);
}
catch (Exception ex)
{
AlnErrorHandler.HandleError(ex);
}
}
For registering control:
protected void gvSession_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow grdrow in gvSession.Rows)
{
LinkButton Preview = (LinkButton)grdrow.FindControl("lbPreview");
ScriptManager current = ScriptManager.GetCurrent(Page);
if (current != null)
current.RegisterPostBackControl(Preview);
}
}
enter code here
set trigger for your event. put this at end of ContentTemplate
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lbPreview" EventName="Click" />
</Triggers>
Replace your old event with this
protected void gvSession_DataBound(object sender, GridViewRowEventArgs e)
{
LinkButton Preview = e.Row.FindControl("lbPreview") as LinkButton;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(Preview);
}
and assign gvSession_DataBound event in GridView as DataBound event

page load with child grids taking too much of time in loading the page

I have three grids - Category grid, Items grid and Sub Items Grid. I have category grid (grdCategories) and for each category there are many items which i bind to child grids (grdItems). For every item there are many sub items which i display in sub child grid (grdSubItems). Now the problem is that page takes too much of time in loading the data. My HTML Code is below:
It takes even minutes to load the data:
ASPX
<asp:GridView ID="grdCategories" runat="server" AutoGenerateColumns="false" DataKeyNames="Category"
CssClass="menu_items" OnRowDataBound="grdCategories_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="pnlMealOptionsHeader" runat="server" Width="100%">
<h1>
<a id='<%# Eval("CategoryX")%>'>
<asp:Label ID="lblCategory" runat="server" Text='<%#Eval("Category") %>' Visible="false"></asp:Label>
<asp:Label ID="lblCategoryX" runat="server" Text='<%#Eval("CategoryX") %>'></asp:Label>
</a>
</h1>
</asp:Panel>
<asp:Panel runat="server" ID="pnlMealOptionsBody">
<asp:Label ID="lblCategoryXX" runat="server" CssClass="title_2" Text='<%#Eval("CategoryXX") %>'></asp:Label>
<!--Items in Category -->
<asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="false" CssClass="active-grid"
DataKeyNames="Item" OnRowDataBound="grdItems_RowDataBound" ShowHeader="false" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblItem" runat="server" Text='<%#Eval("Item") %>' Visible="false"></asp:Label>
<asp:Label ID="lblItemX" CssClass="title" runat="server" Text='<%#Eval("ItemX") %>'></asp:Label>
<asp:Label ID="lblItemXX" CssClass="title" runat="server" Text='<%#Eval("ItemXX") %>' style=" font-size:smaller; font-weight:normal"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="grdSubItems" runat="server" AutoGenerateColumns="false" CssClass=""
DataKeyNames="SubItem" ShowHeader="false" OnRowDataBound="grdSubItems_RowDataBound">
<HeaderStyle HorizontalAlign="Left" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblItem" runat="server" Text='<%#Eval("Item") %>' Visible="false"></asp:Label>
<asp:Label ID="lblSubItem" runat="server" Text='<%#Eval("SubItem") %>' Visible="false"></asp:Label>
<asp:Label ID="lblNoofOptions" runat="server" Text='<%#Eval("NumofOptions") %>' Visible="false"></asp:Label>
<asp:Label ID="lblSubItemX" CssClass="qty" runat="server" Text='<%#Eval("SubItemX") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" CssClass="price" Text='<%#String.Format("£{0}",Eval("SellingCost")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--End Items in Category -->
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<table width="900px">
<tr>
<td align="center">
<h1>
No Data Available</h1>
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
Code Behind
Categories Gridview Bind
private void FillCategoriesGrid()
{
DataSet ds = new DataSet();
ShopCategoryMapBL bl = new ShopCategoryMapBL(SessionContext.SystemUser);
bl.FetchForShop(ds, RowId);
grdCategories.DataSource = ds.Tables[0].DefaultView;
grdCategories.DataBind();
}
Items Gridview Bind
protected void grdCategories_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView chkTopings = e.Row.FindControl("grdItems") as GridView;
Label lblCategory = e.Row.FindControl("lblCategory") as Label;
FillItemsGrid(chkTopings, WebHelper.Cast(lblCategory.Text, 0));
}
}
protected void FillItemsGrid(GridView grdItems, int Category)
{
try
{
//int cleanOrder = CargoBag.GetValue("CleanOrder", 0);
DataSet aDataSet = new DataSet();
ItemBL bl = new ItemBL(SessionContext.SystemUser);
bl.FetchForCategory(aDataSet, Category, RowId);
grdItems.DataSource = aDataSet.Tables[0];
grdItems.DataBind();
}
catch (Exception ex) { }
}
Sub Items Gridview Bind
protected void grdItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grdSubItems = e.Row.FindControl("grdSubItems") as GridView;
Label lblItem = e.Row.FindControl("lblItem") as Label;
FillSubItemsGrid(grdSubItems, WebHelper.Cast(lblItem.Text, 0));
}
}
protected void FillSubItemsGrid(GridView grdSubItems, int Item)
{
try
{
//int cleanOrder = CargoBag.GetValue("CleanOrder", 0);
DataSet aDataSet = new DataSet();
SubItemBL bl = new SubItemBL(SessionContext.SystemUser);
bl.FetchForItem(aDataSet, Item);
grdSubItems.DataSource = aDataSet.Tables[0];
grdSubItems.DataBind();
}
catch (Exception ex) { }
}
You need to provide all of your code before we can provide an accurate answer.
For example:
Where SubItemBL is defined?
Also try to see what line is the exact bottleneck.
Is it
bl.FetchForCategory(aDataSet, Category, RowId);
or
grdSubItems.DataBind();
If above line is the bottleneck, note that Gridview binding to a datasource with large number of data is indeedslow. how large is your data?

Categories

Resources