I have a relatively simple question about nested DataList.
How to dynamically nest infinite DataLists?
I didn't write any code yet, because I don't know how, but follow is an example:
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button2" runat="server" Text="Button" />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
</form>
There are no problems when we have limited levels depth, we just nest two or three levels on DataList and we are done. But when we don't have a defined levels depth.
When the button clicked it loads the DataList2, when DataList2 button is clicked and it should load DataList3 and so far. And repeat it every time I click the button inside a DataList.
e.g.
-DatalistOUTER
--Some bindings
--Button [clicked]
[loads]
----DatalistINNER
-----Some bindings
-----Button [clicked]
[loads]
------DatalistINNERINNER
-------Somebindings
-------Button
Related
I have a repeater with which I am able to display item. Here the code:
<asp:Repeater
ID="rptHardware"
runat="server">
<ItemTemplate>
<div class="Hardware">
<h1><%#Eval("Name") %></h1>
<br />
<asp:Image runat="server" ImageUrl='<%# "/uploadedimg/" + Eval("ImageName") %>' Width="70%" />
</div>
</ItemTemplate>
</asp:Repeater>
I want to go in a detail page when I click on an item. Where do I put the postback URL? This is what I came up with.
PostBackUrl='<%#Eval("Hardware_ID","bookingdetails.aspx?id={0}")%>'
you can put it on H1 tag of Name section as well like below
<asp:Repeater
ID="rptHardware"
runat="server">
<ItemTemplate>
<div class="Hardware">
<a href='bookingdetails.aspx?id=<%#Eval("Hardware_ID")%>'><h1><%#Eval("Name") %></h1></a>
<br />
<asp:Image runat="server" ImageUrl='<%# "/uploadedimg/" + Eval("ImageName") %>' Width="70%" />
</div>
</ItemTemplate>
</asp:Repeater>
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>
Maybe I am not understanding how this works completely but what I have is a gridview custom template with an imagebutton and a modalpopup. Below my page I have my popup panel which is hidden. When I click my image button the panel displays then I click the "btnModalCancelAll" and it starts to step through my method which sets a page variable from 0 to 1 but then the problem comes in it does not continue to the original gridview image click event not sure why. Below is my template field and the popup panel.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButtonCancelResv" runat="server" Width="20" Height="20px" ImageUrl="~/Images/Delete.png" OnClick="ImageButtonCancelResv_Click" />
<asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="ImageButtonCancelResv" PopupControlID="pnlModalPanel" CancelControlID="btnModalCancel"></asp:ModalPopupExtender>
<br />
<asp:Label ID="Label1" runat="server" Text="Cancel"></asp:Label>
</ItemTemplate>
<asp:Panel ID="pnlModalPanel" CssClass="modalBackground" runat="server" Style="display:none">
<asp:UpdatePanel runat="server" ID="updatePanelPopUp">
<ContentTemplate>
<div id="modalWrap">
<asp:Label ID="lblAreyousure" runat="server" Text="Are you sure you want to do that?"></asp:Label>
<div id="divhr1" class="horizontalRule" runat="server"></div>
<div id="divCancelSummaryAll">
<asp:Label ID="lblEntireFamily" runat="server" Text="Cancel all family reservations from this event date."></asp:Label><br />
<asp:Label ID="lblSummaryDeleteAll" runat="server" Text=""></asp:Label><br />
<asp:Button ID="btnModalDeleteAll" runat="server" CssClass="modalButton" Text="Cancel all Family" OnClick="btnModalDeleteAll_Click" />
</div>
<div id="divCancelSummaryOne">
<asp:Label ID="Label2" runat="server" Text="Cancel selected child reservation from this event date."></asp:Label><br />
<asp:Label ID="lblSummaryDeleteOne" runat="server" Text=""></asp:Label><br />
<asp:Button ID="btnModalDeleteOne" runat="server" CssClass="modalButton" Text="Cancel this Child" OnClick="btnModalDeleteOne_Click" />
</div>
<br />
<asp:Button ID="btnModalCancel" runat="server" Text="Cancel" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
I suggest you create another button and use it as the TargetControlID for the modal popup.
you can hide this button by setting its display to 'none'. This will only be a dummy button so that your code compiles.
Now when you click the button "ImageButtonCancelResv" it will run your code which is in the method OnClick="ImageButtonCancelResv_Click"
and at the end of this method you can add a command to show the popup:
mpe.Show();
Example Code:
protected void ImageButtonCancelResv_Click(object sender, EventArgs e)
{
//Do something here when button is clicked
mpe.Show();
}
And then
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButtonCancelResv" runat="server" Width="20" Height="20px" ImageUrl="~/Images/Delete.png" OnClick="ImageButtonCancelResv_Click" />
<div style="display: none">
<asp:Button ID="btnModalStatusHidden" runat="server" />
</div>
<asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="btnModalStatusHidden" PopupControlID="pnlModalPanel" CancelControlID="btnModalCancel"></asp:ModalPopupExtender>
<br />
<asp:Label ID="Label1" runat="server" Text="Cancel"></asp:Label>
</ItemTemplate>
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.
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.