I have a GridView with the following column:
<asp:HyperLinkField DataNavigateUrlFields="agent_level" DataNavigateUrlFormatString="agent_production.aspx?agentlevel={0}" DataTextField="agent_id" HeaderText="View Agents" DataTextFormatString="View" Text="View"/>
I have been looking for a way to do the following: if the agent_level = 'b' (this value is from the database) then the "View" is not clickable.
You can use TemplateField for small business logic. If you want complex business logic, you want to consider using OnRowDataBound event.
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="View Agents">
<ItemTemplate>
<asp:HyperLink
NavigateUrl='<%# Eval("agent_level").ToString() == "b"
? "javascript:void(0)"
: "agent_production.aspx?agentlevel=" + Eval("agent_id") %>'
runat="server"
ID="AgentHyperlink" Text="View" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Related
I am a beginner in learning asp.net. I have a column in a GridView with header name FORM ID. I want to be able to navigate to the different URL based on the part of the FORM ID.
For example,
Clicking on abc10001 will take us to "~/abc1.aspx?formid=abc10001"
Clicking on abc20001 will take us to "~/abc2.aspx?formid=abc20001"
I understand the use of the MID function like so v=MID(string,4,1) to capture the 4th value and redirect to page by determining the value v but I do not know how to apply this correctly. Please guide me. Your help is greatly appreciated.
The following is the aspx code I'm currently work on :
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">
<columns>
<asp:Hyperlinkfield DataTextField="formid" HeaderText="Form ID" ItemStyle- Width="150px"
DataNavigateUrlFields="formid" DataNavigateUrlFormatString="~/abc1.aspx" />
</Columns>
</asp:GridView>
You could switch your Hyperlinkfield to a TemplateField with a HyperLink control to give you more control over the NavigateUrl like so:
<asp:TemplateField HeaderText="Form ID">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%# Eval("formid") %>'
NavigateUrl='<%# "~/abc" + Mid(Eval("formid"), 4, 1) + ".aspx" %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
This is my final code which is working successfully :
<asp:GridView ID="child" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Form ID" >
<ItemTemplate>
<asp:Hyperlink runat="server" Text='<%# Eval("formid") %>'
NavigateUrl='<%# Eval("formid","~/abc" + Mid(Eval("formid"), 4, 1) + ".aspx?formid={0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Credits to #mason and #Tony L. for helping me out on this issue.
I am trying to add a column with dropdownlist to a DataGrid
Here is the code for the DataGrid binding datasource
List<CPDEmployee> employeelist = (List<CPDEmployee>)Cache["EmployeeList"];
unverifiedlist.DataSource = employeelist;
unverifiedlist.DataBind();
unverifiedlist.AllowPaging = true;
unverifiedlist.PageSize = 10;
In the page.aspx code is like this
<asp:DataGrid ID="unverifiedlist" runat="server" AllowPaging="true" PageSize="10" OnPageIndexChanged="unverifiedlist_PageIndexChanged">
<Columns>
<asp:BoundColumn HeaderText="Surname" DataField="Surname" ReadOnly="true">
</asp:BoundColumn>
</Columns>
<Columns>
<asp:TemplateColumn HeaderText="Options" >
<ItemTemplate>
<asp:DropDownList ID="options" runat="server">
<asp:ListItem Value="1">Verified</asp:ListItem>
<asp:ListItem Value="0">Rejected</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText ="Reason">
<ItemTemplate>
<asp:TextBox ID="reason" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
The result is there are two "Surname" fields in the datagrid. The reason I added "Surname" from page side is that i can't set the order of the columns (the dropdownlist should be at last).
Does anyone know how to solve this problem? thanks very much
You have to turn off AutoGenerateColumns and specify all the bound fields.
Lets say I have a drop down list and a grid view on a page like this
<asp:GridView ID="gvCategories" runat="server" >
<Columns>
<asp:HyperLinkField DataTextField="CategoryName" DataNavigateUrlFields="CategoryID" DataNavigateUrlFormatString="~/Learning.aspx?categoryID={0" />
</Columns>
</asp:GridView>
I'd like to add this to the URL:
&view=<%=SelectedDropdownlistvalue%>
How can I do this?
I would use TemplateField as:
<asp:GridView ID="gvCategories" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("CategoryName", "~/Learning.aspx?categoryID={0}")+" &view=" + DropDownList1.SelectedValue %>'
Text="Goto Page"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Remember the DropDownList.SelectedValue is obtained at postBack, so the link is constructed at that time. To change the link dynamically when the value of the dropdownlist changes set the AutoPostBack="true" of the dropdownlist.
I have the following markup in an ASP.NET page:
<asp:GridView ID="gv" runat="server" DataSourceID="ods" OnRowDataBound="gv_RowDataBound">
<columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbx" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
How can I validate the TextBox only if the CheckBox is checked?
I searched for similar situations, but couldn't find anything.
Thanks.
U can use RowCommand under which u can get the control status using int index = AuthorsGridView.EditIndex;
GridViewRow row = AuthorsGridView.Rows[index]; and then validate the text box
refer this http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.aspx
I have a GridView with a BoundField column and quite a few item templates like the following:
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" OnRowCommand="gvUsers_RowCommand"
OnRowDataBound="gvUsers_RowDataBound" DataKeyNames="UserId" OnRowEditing="gvUsers_OnRowEditing"
OnRowUpdating="gvUsers_OnRowUpdating" OnRowUpdated="gvUsers_OnRowUpdated"
DataSourceID="DataSource1" Width="807px" Height="105px"
AllowPaging="True" >
<Columns>
<asp:BoundField DataField="UserName" HeaderText="User Name"
SortExpression="UserName" />
<asp:TemplateField HeaderText="Approver">
<ItemTemplate>
<asp:CheckBox ID="cbApprover" runat="server" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Auditor">
<ItemTemplate>
<asp:CheckBox ID="cbAuditor" runat="server" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Edit" Text="Edit" />
<asp:Label ID="lblPipe1" runat="server" Text=" | " />
<asp:LinkButton ID="btnUpdate" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Update" Text="Update" />
<asp:Label ID="lblPipe" runat="server" Text=" | " />
<asp:LinkButton ID="btnDelete" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Remove"
OnClientClick="return confirm('Are you sure you want to delete this user?');"
Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
While handling the Edit link button I need to use the value in the BoundField, UserName. Unfortunately during the OnRowEditing handler, all strings are empty "". This is also true in the ensuing OnRowDataBound handler for the row in question where e.Row.RowState == DataControlRowState.Edit. Is there a way to obtain this value after the user clicks on the Edit link, and commences the OnRowEditing event?
I arrived at a solution that was a bit more complicated than I was hoping for. I'll show code later but I did the following:
Created a HiddenField to hold the
value of the cell I wanted to edit in
its view state.
In the OnRowDataBound handler, I assigned the value to the
HiddenField.
The ItemTemplate, as seen above, sends the UserName as an
argument. I store this value in
the HiddenField.
The OnRowEditing is fired after the Command handler. That is where I
read the HiddenField.
If anyone has a simpler solution I would love hear about it.
Could it be that you're using late binding in TemplatedItems without EditItemTemplates?