user control inside dynamic user control click event not firing? - c#

I have a page where there is a div, for which the user controls are added dynamically through the code using the LoadControl of user control.
The user control contains another user control (lets say ChildUC).
The ChildUC contains a gridview which has Insert and Delete buttons.
The problem is, even though I click on these buttons, the click event is not getting fired nor the gridview command event.
The page is being postbacked and just exits after the page load, it is not firing any other events.
I am debugging this for last few days so far, but of no luck.
Please do help me. Thanks in Advance.
Code:
<div id="dvMain" runat="server">
</div>
private void CreateHeaders()
{
foreach (Header hd in HeaderCollection)
{
List<Question> lstQuestion = QuestionCollection.FindAll(p => p.HID.Equals(hd.HeaderID));
Sales_UserControls_Questions Question = null;
if (lstQuestion.Count > 0)
{
Question = (Sales_UserControls_Questions)LoadControl("~/Sales/UserControls/Questions.ascx"); // This is the main user control that is being loaded dynamically
Question.ID = "Question"+hd.HID;
Question.lstQuestions = lstQuestion;
}
dvMain.Controls.Add(Header);
if (HQQuestion != null)
dvMain.Controls.Add(Question); // user control being added to div
}
}
//Questions.ascx
<div>
<asp:GridView ID="grdContent" runat="server" AutoGenerateColumns="False"
OnRowDataBound="grdContent_RowDataBound" EnableTheming="False">
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:HiddenField ID="hdnQuestionID" runat="server" Value='<%# Eval("QID") %>' />
<asp:HiddenField ID="hdnQuestionType" runat="server" Value='<%# Eval("QTP") %>' />
<asp:Label ID="lblQuestionID" runat="server" EnableTheming="False" Text='<%# Eval("QID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblSHR" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblText" runat="server" Text='<%# Eval("Text") %>'></asp:Label>
<div>
<asp:UpdatePanel ID="UpnlDGV" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<DG:View ID="ucDGView" runat="server"></DG:View>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
//end of questions.ascx
//ucDGView.ascx
<div>
<asp:GridView ID="grdDGView" runat="server"
AutoGenerateColumns="False" OnRowDataBound="grdDGView_RowDataBound" OnRowDeleting="grdDGView_RowDeleting"
OnRowCreated="grdDGView_RowCreated" HeaderStyle-Font-Bold="true" OnRowCommand="grdDGView_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<dl id="dvNT" runat="server">
<dt>
<asp:Label ID="lblNT" runat="server" Text="NT:"></asp:Label>
</dt>
<dd>
<asp:DropDownList ID="ddlNM" runat="server">
</asp:DropDownList>
</dd>
</dl>
<dl id="dvOSD" runat="server" class="grid_6 alpha MT10" visible="false">
<dt>
<asp:Label ID="lblOSDT" runat="server" Text="OSDT:"></asp:Label>
</dt>
<dd>
<asp:TextBox ID="txtOSDT" runat="server" MaxLength="10" CssClass="maskDate"></asp:TextBox>
<asp:CalendarExtender ID="txtOSDTE" runat="server" TargetControlID="txtOSDT"
Enabled="True" Format="MM/dd/yyyy">
</asp:CalendarExtender>
</dd>
</dl>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgBtnDelete" runat="server" ImageUrl="~/Sales/Common/Images/delete_red_mini.jpg"
CausesValidation="false" CssClass="cancel" CommandName="Delete" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "OID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgBtnAdd" runat="server" ImageUrl="~/Sales/Common/Images/PLUS_MINI.png"
CausesValidation="false" CssClass="cancel" CommandName="Add" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "QID")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
//end of ucDGView.ascx

What you need to understand here is where in the asp.net page life cycle does it receive the event from the frontend.
The event that triggers the postback is sent back to asp.net is a hidden form element called the event target.
This input is processed in in a function called the ProcessPostData.
If your control is not present in the controls collection by then, it cannot receive the event. So you get a postback, but no event handlers are fired.
That means, if you want your event handlers to fire, you must create all your controls before the load event. It seems you have to decide which controls to load depending upon some user input. User inputs are not available on Init. You can override the LoadViewState method, and process your user input there.

Related

Failed to Load Viewstate Error after editing Gridview in ModalPopupExtender

I am getting the following error.
HttpStatusCode:500
Name:Sys.WebForms.PageRequestManagerServerErrorException
Message: Sys.WebForms.Page.RequestManagerServerErrorException
Failed to Load viewstate. The control tree into which viewstate is being
loaded must match the control tree that was used to save viewstate during
the previous request.
The scenario is as follows I have a Gridview that is bound after a DropDownList is selected. Then you can click a row on the Gridview and a ModalPopupExtender pops up. There is another GridView in the popup. If I edit that Gridview and then exit the ModalPopupExtender, and then select a different value on the DropDownList which generates the original Gridview I get the error.
DropDownList
<asp:DropDownList ID="SearchCategoryDD" runat="server" DataSourceID="InductionCategoriesDS" DataTextField="CompentencyCategory" DataValueField="CompentencyCategoryID" AutoPostBack="True" OnDataBound="SearchCategoryDD_DataBound" OnSelectedIndexChanged="SearchCategoryDD_SelectedIndexChanged"></asp:DropDownList>
Gridview when Databound with new data causes error
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
<asp:GridView runat="server" ID="SkillsXXX" OnRowDataBound="SkillsXXX_OnRowDataBound" >
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton runat="server" ID="SelectLBXX" Visible="False" OnClick="SelectLBXX_OnClick"></asp:LinkButton>
<asp:Label runat="server" ID="EmployeeXXXXD" Text='<%# Bind("EmployeeID") %>' Visible="False"></asp:Label>
</EditItemTemplate>
......
...
ModalPopupExtender with Gridview
<asp:Button ID="EditSupplierContactPopupBTN" runat="server" Text="" Style="visibility: hidden;" />
<ajaxToolkit:ModalPopupExtender ID="EditSupplierContactMPE" runat="server" CancelControlID="EditSupplierContactCancelBTN"
TargetControlID="EditSupplierContactPopupBTN" PopupControlID="EditSupplierContactPanel" PopupDragHandleControlID="EditSupplierContactHeader"
Drag="true" BackgroundCssClass="ModalPopupBG">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="EditSupplierContactPanel" runat="server" CssClass="PopupPNL" Style="display: none"> <!-- Style="display: none" -->
<div class="PopupHeader" id="EditSupplierContactHeader">
<div class="PopupControls">
<asp:Button ID="EditSupplierContactCancelBTN" runat="server" Text="Close" />
</div>
Edit Truck Driver
</div>
<div class="PopupBody">
<asp:UpdatePanel ID="UpdatePanel3" runat="server"><ContentTemplate>
<asp:GridView ID="Competancies2GV" runat="server" AutoGenerateColumns="False" DataKeyNames="SkillsMatrixID" Width="100%">
<Columns>
<asp:TemplateField InsertVisible="False" SortExpression="SkillsMatrixID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("SkillsMatrixID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="EditSkillsMatrixLB" runat="server" CommandArgument='<%# Eval("SkillsMatrixID") %>' OnCommand="EditSkillsMatrixLB2_Command">Edit</asp:LinkButton>
<asp:HiddenField runat="server" ID="SkillsMatrixIDHF" Value='<%# Bind("SkillsMatrixID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompentencyCategory" HeaderText="Category" SortExpression="CompentencyCategory">
</asp:BoundField>
<asp:BoundField DataField="CompentencyName" HeaderText="Compentency" SortExpression="CompentencyName">
</asp:BoundField>
...........
........
.....
I have tried the loading the Datatable from ViewState in the LoadViewState method. I still get the error.
LoadViewState
protected override void LoadViewState(object earlierState)
{
base.LoadViewState(earlierState);
dataTable = (DataTable)ViewState["dataTable2"];
SkillsMatrixGV.DataSource = dataTable;
compentencyDataTable = (DataTable) ViewState["CompentencyDataTable2"];
Competancies2GV.DataSource = compentencyDataTable;
Competancies2GV.DataBind();
......
...
if I put EnableViewState="false" into the page or the GridView the error goes away but my GridView in the ModalPopupExtender does not update.

Place RequiredFieldValidator inside a asp:DataList

I have a problem with placing RequiredFieldValidator into a DataList, and some assistance would be greatly appreciated.
Problem description:
When I place the <asp:RequiredFieldValidator>inside the <ItemTemplate> and run the page I get the following error page:
When I place it outside of it, the page works with no error, but obviously it cannot identify witch <asp:TextBox> does not have content in it, before it is posted back. And I would like to make where it is able to tell if a particular text box in the data list does not have text entered.
Here is the markup: (the Image src attribute is not implemented yet)
<asp:DataList ID="imageUploadRoster" runat="server"
DataSourceID="ImageUploadRosterDataSource" RepeatDirection="Horizontal" RepeatColumns="5" HorizontalAlign="Left">
<HeaderTemplate>
<h3>Set Image Names</h3>
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="180px" Width="180px" />
<ItemTemplate>
<div class="imageSetNameDiv">
<asp:HiddenField ID="ImageId" runat="server" Value='<%# Eval("ImageId") %>' />
<asp:HiddenField ID="ImageMimeTypeLabel" runat="server" Value='<%# Eval("ImageMimeType") %>' />
<asp:Image ID="ImageThumbnailLabel" runat="server" Src='<%# Eval("ImageThumbnail") %>' Width="120px" Height="120px" />
<br />
<asp:RequiredFieldValidator ID="imageNameRequired" runat="server"
ControlToValidate="ImageName" ErrorMessage="RequiredFieldValidator"
ValidationGroup="imageUploadValid">
</asp:RequiredFieldValidator>
<asp:TextBox ID="ImageName" runat="server" Text='<%# Eval("ImageName") %>' />
<br />
<hr />
<asp:Button ID="removeImage" runat="server" Text="Remove" CommandName="delete" CommandArgument='<%# Eval("ImageId") %>' />
</div>
</ItemTemplate>
</asp:DataList>
Thank you in advance for any assistance.
Peter
This error happens when ASP.NET encounters two controls with the same ID on the page. I guess for some reason in your case validators for each item get the same ID. Setting the following property:
ClientIDMode="Predictable"
for validator should fix it. This mode makes sure control in databound context gets correct ID.

Display table content using querystring

I have one page in asp, contain three table ( Pending order, confirmed order and rejected order). This three table under one header. When user click on pending order word on header, page will display only pending table. This three information in on code page using asp. How can get this function work using the query string?
Detail:
As refer image below, current state all the information in one page,
The request is when user click on the pending order, it only display the pending table, also apply to confirm and rejected.
Each table using grid view function and i want to use the query string. If I use to create separate page for this,it will involve to many definition.
Update the code:
The code for the every table i use grid view in the aspx file.
<div class="col-md-12 account-title">
Your Pending Orders Confirmation
</div>
<div class="col-md-12 col-sm-12 table-responsive">
<asp:GridView ID="grdvwPendingOrder" ShowFooter="false" AutoGenerateColumns="False" runat="server" CssClass="table table-bordered text-center"
OnRowCommand="grdvwPendingOrder_RowCommand" OnRowDataBound="grdvwPendingOrder_RowDataBound" EmptyDataRowStyle-ForeColor="Red"
EmptyDataRowStyle-BackColor="White" EmptyDataText="No Records Found">
<HeaderStyle CssClass="table-title text-center" />
<RowStyle CssClass="account-content text-center" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>Order No.</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblOrderNo" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Date</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OrderDate", "{0:dd/MM/yyyy}") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Placed By</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPlacedby" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustomerName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Status</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OrderStatus") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Actions</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="btnReview" runat="server" CssClass="btn btn-success" Text="Review" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' CommandName="Review" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<!-- Pagination -->
<div class="col-md-12 col-sm-12 col-xs-12 padLROff">
<ucPageControl:PageControl ID="PendingOrderPageControl" Title="" runat="server" Visible="false" />
</div>
Code for pending order.
The confirmed and rejected in the same file.
Now I want to build a header for this type as table above. As mention, if i separate the type to different aspx file, it will involve to many definition.

ASP.NET repeater get all items and checkbox if user has right

I have an ASP.NET application and I would like to display all roles and add a checkbox which is checked if the user has the right.
I use an ASP repeater to get all roles but how can I check the checkbox in the same repeater to get the user's role ?
Here is my code:
<asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
<ItemTemplate>
<div>
<asp:CheckBox runat="server" Checked="False" />
<asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
<asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
<ItemTemplate>
<div>
<asp:CheckBox runat="server" Checked='<%# Convert.ToBoolean(Eval("role")) %>' />
<asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
Make sure you get 'role' field 1 or 0 from DB
Thanks Hardik,
based on your solution, I created a similar method like this: public Dictionary GetAllRolesFromUserToDictionary() so the checkbox will bind to the boolean value
Then the ASP code is like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource3">
<ItemTemplate>
<div>
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("Value")%>' />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Key")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource3" runat="server" SelectMethod="GetAllRolesFromCurrentUserToDictionary" TypeName="BusinessObject.RoleCollection"></asp:ObjectDataSource>

ModalPopupExtender closes when UpdatePanel inside is updated

I have a ModalPopupExtender with an UpdatePanel inside. The UpdatePanel has a Repeater with a list of LinkButtons.
<asp:Button ID="btnShow" runat="server" />
<ajaxToolkit:ModalPopupExtender ID="mpe" runat="server" TargetControlID="btnShow" PopupControlID="pnl" CancelControlID="btnCancel" />
<asp:Panel ID="pnl" runat="server">
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rep" runat="server" onitemcommand="rep_ItemCommand">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:Label ID="lblAssignedTo" runat="server" Text='<%# Eval("AssignedTo") %>' />
<asp:LinkButton ID="lnkUnassign" runat="server" CommandName="Unassign" CommandArgument='<%# Eval("Id") %>' />
<ajaxToolkit:ConfirmButtonExtender ID="cbeUnassign" runat="server" TargetControlID="lnkUnassign" ConfirmText="Are you sure you want to unassign this item?" />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Unassign")
{
//do something
up.Update();
}
}
When I click on a LinkButton, the UpdatePanel should update. It does this but it closes the ModalPopupExtender also.
Is there any way to update the UpdatePanel without hiding the ModalPopupExtender? I can just call ModalPopupExtender.Show(), but the page flickers.
Thanks.
Okay, just found out that there's something wrong with LinkButtons inside Repeaters. Used a Button control instead.

Categories