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

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>

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.

Checkbox checked issue in grid view

In the below code i have a grid view in which i have a textbox i am passing 0 and 1 to check box if it is 1 the checkbox to be checked.I tried but it is not working.Pls help me to solve the issue.
<asp:TemplateColumn HeaderText="Sales Price Ref" ItemStyle-Width="200px" HeaderStyle-VerticalAlign="Top">
<HeaderTemplate>
<asp:Label ID="lblSalesPriceRef" runat="server" Text="Sales Price"></asp:Label>
<br /><br />
<asp:TextBox runat="server" ID="txtSalesPriceRef" AutoPostBack="true" BorderStyle="Solid" BorderColor="#6495ED" BackColor="#B0C4DE" Height="20px" Width="90px" OnTextChanged="txtItem_TextChanged"></asp:TextBox>
</HeaderTemplate>
<EditItemTemplate>
<asp:CheckBox ID="ChSalesPriceRef" runat="server" Checked='<%#Eval("SalesPriceRef")=="1" ? true:false %>' />
<%-- <asp:TextBox ID="txtSalesPriceRef" Width="90px" runat="server" Text='<%#Eval("SalesPriceRef") %>'></asp:TextBox>--%>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSalesPriceRef" runat="server" Text='<%# Convert.ToString(Eval("SalesPriceRef"))== "1" ? "True" :"False" %>' > </asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
YES you can achieve it by binding it to the checkbox
as
<asp:CheckBox ID="ChSalesPriceRef" runat="server" Checked='<%# Bind("SalesPriceRef")%>' Enabled="false" />
if you allow to change checkbox value then make enable=true

Making only certain cells in a gridview column linkbuttons

I have a gridview which pulls data from a database:
I wan't to make the content within task a linkbutton that can be clicked to show a popup with additional informatiom.
When I make the column linkbuttons it is also making Total: and Subtotal: a link button:
<asp:TemplateField HeaderText="Task" ItemStyle-Width="20%">
<ItemTemplate>
<asp:LinkButton ID="taskLinkButton" runat="server"
Text='<%# Eval("Task")%>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
How do I prevent these specific rows from being link buttons?
A bit dirty but this should do the trick:
So i coulnt get to call Databinder.Eval inside <% %>, I'm not sure if it's actually possible, so I went with a different solution. The following worked for me (and its even shorter than last version):
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text='<%# Eval("task") %>' Visible='<%# Eval("incidentN") != null %>' runat="server" />
<asp:Literal Text='<%# Eval("task") %>' Visible='<%# Eval("incidentN") == null %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
You can use a PlaceHolder with conditional visibility
<asp:PlaceHolder ID="Ok" runat="server" Visible='<%# (Eval("LoadStatus").ToString()=="false"?true:false) %>'><%----%>
<asp:Label ID="Label1" Text='<%# Eval("Task")%>' runat="server" />
</asp:PlaceHolder>
<asp:PlaceHolder ID="Ko" runat="server" Visible='<%# (Eval("LoadStatus").ToString()=="false"?false:true) %>'><%----%>
<asp:LinkButton ID="taskLinkButton" runat="server"
Text='<%# Eval("Task")%>'>
</asp:LinkButton>
</asp:PlaceHolder>

user control inside dynamic user control click event not firing?

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.

DetailsView and EditItemTemplate

me problem is next:
I have a DetailsView control with many editItemTemplates some work fine but i have one dont work's.
code here:
<asp:TemplateField HeaderText="Date" SortExpression="date">
<ItemTemplate>
<%#this.putDate(Eval("fecha")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="date" runat="server" ></asp:TextBox>
<asp:CalendarExtender id="CE" runat="server" Format="dd/MM/yyyy"
PopupButtonID="calImg" TargetControlID="date">
</asp:CalendarExtender>
<asp:ImageButton id="calImg" runat="server" CommandName=""
ImageUrl="img/imgCal.gif" />
</EditItemTemplate>
</asp:TemplateField>
In view mode the information appear right but in edit mode after select new date with calendar and pres accept to update information, the data dont update. Any idea?
Thz!
Replace:
<asp:TextBox ID="date" runat="server" ></asp:TextBox>
With:
<asp:TextBox ID="date" runat="server" Text='<%# Bind("SelectedDate") %>'></asp:TextBox>

Categories