Multiple DataNavigateUrlFields... one from datasource, one from drpdownlist - asp.net

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.

Related

How to add if statement logic in ASP.Net GridView Column

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>

How can I navigate to a different URL when user clicks the hyperlink in the GridView with a condition?

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.

How to add checkbox to datagrid in vb.net

I have a datagrid with a set of columns showing data from a database. I create the datatable and add it to the datagrid and then bind the source. this works great and now I would like to add a column to the front of the grid that has checkbox in it.
Do I add the checkbox when I am adding the new row to the datatable that is shown in the datagrid or after I databind the datatable to the datagrid?
Using: VB.Net, Visual Studio 2012
you can add checkbox using template field
Set AutoGenerateColumns attribute to false.
Add Column tag to asp:DataGrid tag.
Now add itemtemplate inside columns
<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:DataGrid>
and if you want to attach it to datatable column then u have to add like this
<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkStatus_OnChackedChanged" Checked='<%# Convert.ToBoolean(Eval("Approved")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:DataGrid>

passing a querystring from one of the griditems bound columns

I have a gridview, one of the item in the gridview is
<asp:GridBoundColumn DataField="Id" UniqueName="Id" DataType="System.Int32" Visible="false"></asp:GridBoundColumn>
and another item is
<asp:ImageButton id="RadButton_RunQuery" ImageUrl="~/images/run_query_button.jpg" PostBackUrl="~/Viewer/ViewerSummary.aspx?QueryID=" runat="server" />
want to pass Id from the asp:GridBoundColumn as a querystring to the postbackurl of the asp:imageButton.
How can I achieve this?
You can try with this code
PostBackUrl='<%# "~/Viewer/ViewerSummary.aspx?QueryID=" + DataBinder.Eval(Container.DataItem,"ID") %>'
Since you are using an ImageButton then that means you are using an ItemTemplate. You can do something like this:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton id="RadButton_RunQuery" ImageUrl="~/images/run_query_button.jpg"
PostBackUrl='<%#string.Format("~/Viewer/ViewerSummary.aspx?QueryID={0}",Eval("Id")) %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>

Validating a TextBox if a Checkbox is checked (inside GridView)

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

Resources