passing a querystring from one of the griditems bound columns - asp.net

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>

Related

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.

Multiple DataNavigateUrlFields... one from datasource, one from drpdownlist

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.

Hyperlink in datagrid view

I want to set hyperlink field in datagrid view. When user clicks on that link, a query string should be generated and user should be directed to another page. So how can I set hyperlink to generate query string?
<asp:GridView ID="Griddata" runat="server" AutoGenerateColumns="False" CellPadding="1"
GridLines="Horizontal" Width="1000px" ShowFooter="True" CssClass="grid" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:HyperLinkField HeaderText="ID" DataTextField="rec_id" DataNavigateUrlFields="rec_id"
DataNavigateUrlFormatString="followme.aspx?record={0} " />
<asp:BoundField HeaderText="Login" DataField="LoginName"></asp:BoundField>
</Columns>
</asp:GridView>
This is a sample GridView defined in ASP.NET
You need to specify the <asp:Hyperlinkfield> in the column definition.
In that field, you need to specify the DataTextfield (is what will be displayed on screen in that column), your URL (DataNavigateUrlFormatString) and your parameter that you want to use in that URL (DataNavigateUrlFields)
Note: I'm binding to this grid from code-behind, not through a SqlDatAdaptor but the result is the same.
You will get something like this:
you can do like...
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
<a href='page.aspx?id=<#Eval("ID")>'>click</a>

asp.net gridview: How can I have multiple button fields in one column?

I need to create multiple actions for a gridview - say, "Approve", "Deny", and "Return".
I can do this by creating a button field for each action:
<asp:ButtonField ButtonType="Link" CommandName="Approve" Text="Approve" />
<asp:ButtonField ButtonType="Link" CommandName="Deny" Text="Deny />
<asp:ButtonField ButtonType="Link" CommandName="Return" Text="Deny" />
However, this creates one column for each button.
Is there a way to have the same functionality, but combine them into a single column?
Have you considered using an TemplateField? Here is an example:
<asp:GridView ID="grdTest" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnApprove" runat="server" CommandName="Approve" Text="Approve" />
<asp:LinkButton ID="btnDeny" runat="server" CommandName="Deny" Text="Deny" />
<asp:LinkButton ID="btnReturn" runat="server" CommandName="Return" Text="Return" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can then capture the commands the exact same way using OnRowCommand or do whatever you like. You have full control to make it behave how you need and not be bound by the built in functionality of using the regular predefined column types.
Try to put buttons into the <asp:TemplateField> instead:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Approve" Text="Approve" />
<asp:LinkButton CommandName="Deny" Text="Deny />
<asp:LinkButton CommandName="Return" Text="Deny" />
</ItemTemplate>
</asp:TemplateField>
The solution is not to use ButtonField elements.
To accomplish what you want you will need to create a column as a TemplateField and define buttons as regular ASP.NET <asp:Button id="myButton" /> within TemplateField's ItemTemplate or EditItemTemplate as appropriate for your UI.
You can handle Click events in the GridView_OnItemCommand() handler, where you can check e.CommandName to figure out exactly which button caused the event.

How to get cell contents in ASP.NET GridView while handling OnRowEditing?

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?

Resources