Change CommandField text dynamically - c#

<asp:BoundField HearderText="Name" Datafield="Name"/>
<asp:CommandField HearderText="Name" SelectText="Go" ShowSelectButton="True"/>
I want that each CommandField takes the data like DataField, so instead of "Go", it would have values like, "Jhon", "Mary"...in each row

<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="btnSelect" runat="server" CommandName="Select" Text='<%# Eval("Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
This worked for me

Related

Hiding columns in ASP.Net Datagrid

My DataGrid is something like this:
<asp:DataGrid ID="tasks" runat="server" AutoGenerateColumns="False" GridLines="None">
<HeaderStyle CssClass="task-list-header"/>
<ItemStyle CssClass="task-list-row"/>
<Columns>
<asp:BoundColumn DataField="Name"
HeaderStyle-Width="100px"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Description"
HeaderStyle-Width="250px"
HeaderText="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="IsComplete"
HeaderStyle-Width="125px"
HeaderText="Is Complete"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="75px"/>
<ItemTemplate>
<asp:HyperLink ID="Hyperlink1" runat="server"
NavigateUrl='<%# ModuleContext.EditUrl("TaskId", Eval("TaskId").ToString(), "EditTask") %>'
Text="Edit"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle Width="75px"/>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("TaskId") %>' CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
This is basically a list of Task with Last 2 columns as Edit and Delete. I am using DNN8. I want to hide the Edit and Delete links if the user is not logged in as Superuser. How can I do that? I also tried the property OnAutoBinding but no luck.
Thanks, in advance for the help.
You can you like this
e.Row.Cells[columnIndex].Visible = false;

Uniquely identify controls in nested gridview for validation

I have a nested grid. I would like to validate a dropdown control within the child grid when the user adds a record. But in a nested grid, the control IDs are not unique. If you have 2 parent rows and a nested grid under each parent the child grid controls will have the same ID. When I validate, the validation is checking all of the nested grids and not just the one that I am trying to add to.
This is the markup:
<asp:GridView ID="GroupGridView" runat="server" AutoGenerateColumns="False"
Caption="Group Information" CaptionAlign="Top" CssClass="grid"
ShowFooter="true" DataKeyNames="GroupID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:DivExpandCollapse('div<%# Eval("GroupID")%>');">
<img id="imgdiv<%# Eval("GroupID")%>" width="25px" border="0" src="Images/plus.png" /> </a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GroupID">
<ItemTemplate>
<asp:Label ID="uggvLblGroupID" runat="server" Text='<%# Bind("GroupID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group Name">
<ItemTemplate>
<asp:Label ID="uggvLblGroupName" runat="server" Text='<%# Bind("GroupName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="uggvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete" CssClass="gridActionbutton" OnClientClick="return confirm('Are you sure you want to delete this Group Information?')" >
</asp:Button>
<tr><td colspan="100%">
<div id="div<%# Eval("GroupID") %>" style="display:none">
<asp:GridView ID="GroupMemberGridView" runat="server" AutoGenerateColumns="false"
CssClass="grid" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="MemberID">
<ItemTemplate>
<asp:Label ID="mggvLblMemberID" runat="server" Text='<%# Bind("MemberID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Member Name" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:Label ID="mggvLblMemberName" runat="server" Text='<%# Bind("MemberName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="mggvDDLMemberName" runat="server" ClientIDMode="Static"
class="chosen-single" data-placeholder="Choose member…">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqValueDDLMemberInsert" runat="server" InitialValue="0"
ControlToValidate="mggvDDLMemberName" ValidationGroup="'<%# "InsertGroupMemberValidation_" + Eval("GroupID") %>'
ErrorMessage="Selection required." CssClass="message-error-dropdown">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
<FooterTemplate>
<asp:Button ID="mggvAddButton" runat="server" CommandName="Add" Text="Add Member" Width="90%"
CssClass="gridActionbutton" ValidationGroup='<%# "InsertGroupMemberValidation_" + Eval("GroupID") %>'> CausesValidation="true">
</asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="uggvAddButton" runat="server" CommandName="Add" Text="Add Group" Width="90%" CausesValidation="true"
CssClass="gridActionbutton" ValidationGroup="InsertGroupNameValidation"
</asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Regardless of which 'Add Member' button I click, the validation is triggered for all of the nested grids because the Validation Groups are not unique.
How can I uniquely identify the ValidationGroup for each nested grid?
Thanks.
UPDATE
The ValidationGroup identifier works when adding a member to the first nested group but not to subsequent nested grids. It seems it is still going thru all of the nested grids and not just the one from the 'Add' button you clicked.
//Access Validators and Buttons
RequiredFieldValidator RequiredFieldValidator1 = (RequiredFieldValidator)e.Row.FindControl("RequiredFieldValidator1");
RequiredFieldValidator RequiredFieldValidator2 = (RequiredFieldValidator)e.Row.FindControl("RequiredFieldValidator2");
RequiredFieldValidator RequiredFieldValidator3 = (RequiredFieldValidator)e.Row.FindControl("RequiredFieldValidator3");
Button Button = (Button)e.Row.FindControl("Button1");
//Assign validation group to controls.
RequiredFieldValidator1.ValidationGroup = "Gridview1" + e.Row.RowIndex.ToString();
RequiredFieldValidator2.ValidationGroup = "Gridview1" + e.Row.RowIndex.ToString();
RequiredFieldValidator3.ValidationGroup = "Gridview1" + e.Row.RowIndex.ToString();
Button.ValidationGroup = "Gridview1" + e.Row.RowIndex.ToString();

Adding order buttons to a GridView

I have an example from this gridview:
<asp:GridView ID="GridView3" runat="server">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("nome")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And I would like to add a button for ordering the data in each column.
Somebody can help me find the best way to do this?
Sorting is built into the GridView control. There is a control Property called AllowSorting, set this to True and you can sort columns by clicking on the header row titles.
There is no need to add extra buttons, even if you want to do some type of special sorting, you'd still Handle the Sorting event and implement your own methods

Telerik RadGrid Column Filtering on Text Value of LinkButton

<telerik:GridTemplateColumn HeaderText="Code" SortExpression="Code" UniqueName="Code" AllowFiltering="True">
<ItemTemplate>
<asp:LinkButton ID="lbCCode" runat="server" Text='<%# Eval("Code") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
So how can I allow RadGrid to filter the results based on the text value of LinkButton? Is there a way to do that?
Please try with the below code snippet.
<telerik:GridTemplateColumn HeaderText="Code" SortExpression="Code" UniqueName="Code" AllowFiltering="True"
DataField="Code">
<ItemTemplate>
<asp:LinkButton ID="lbCCode" runat="server" Text='<%# Eval("Code") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I have added DataField property in template column. if you get any error in grid then please set EnableLinqExpressions="false" in your grid.

Conditional HyperLink in GridView?

I have a gridview with a hyperlink:
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover table-striped" GridLines="None" >
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="EmployeName">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("EmployeName") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="EmployeID" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("EmployeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
However, it should only appear as a hyperlink if the employeeID is that of the logged in employee.
I can do all that, but what I do not know is how to make the hyperlink look like a Label. It's easy to have it not link anywhere, but I do not know how to make it look like a label.
Thanks
I believe if you set Enabled="false" it does. If it does not, then the only way to do that is put both a HyperLink and Label in the cell, and show the link when appropriate, and the label when appropriate, hiding the other one (which can be easily done in RowDataBound event).

Categories