I have a gridview that is being populated from a datasouce.
The Stored procedure that is populating the datasource, has a field "Client" and a field "Client WebSite".
I want to populate the field "Client" in the gridview column called "Client" which would be a hyperlink field and the hyperlink field would be the "Client WebSite" value from the dataset. The client website is an external site (not within my asp project)
Below is my html code. How can I get the "Client WebSite" appear as the DataNavigatrURL value?
<asp:HyperLinkField DataTextField="Client" HeaderText="Client" DataNavigateUrlFields="Client"
DataNavigateUrlFormatString="Client WebSite">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Left" />
</asp:HyperLinkField>
Use databinding on the NavigateUrl attribute, like this:
NavigateUrl = '<%# Bind("ClientWebSite") %>'
Or more fully:
<asp:HyperLinkField DataTextField='<%# Bind("Client" %>' HeaderText="Client" NavigateUrl='<%# Bind("ClientWebSite") %>'>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizantalAlign="Left" />
</asp:HyperLinkField>
DataNavigateUrlFields is used to gets or set the names of the fields from the data source used to construct the URLs for the hyperlinks in the HyperLinkField object.
'DataNavigateUrlFormatString` is used to gets or sets the string that specifies the format in which the URLs for the hyperlinks in a HyperLinkField object are rendered.
Related
I have a datagrid where I want to have a filed display status as text and trigger a command event so I can handle it server side just like asp:ButtonField do.
My failing code is:
<asp:ButtonField CommandName="CallHist"
ButtonType="Button" Text ="<%#GetJobs(Eval("currStatus"))%>" >
</asp:ButtonField>
I get error:
Literal content ('" >') is not allowed within a
'System.Web.UI.WebControls.ButtonField'.
I am trying to alter existing workign ButtonFields:
<asp:ButtonField CommandName="editRecord" ControlStyle-Height="16px" ControlStyle-Width ="16px" ButtonType="image" ImageUrl="images/edit-icon.png">
</asp:ButtonField>
<asp:ButtonField CommandName="deleteRecord" ControlStyle-Height="16px" ControlStyle-Width ="16px" ButtonType="Image" ImageUrl="images/close-icon.png">
</asp:ButtonField>
I have also tried, with no luck, using <asp:TemplateField>
How can i get a field to display data and be a clickable / fired event?
The ButtonField does not support binding expressions. You can replace it by a TemplateField with a LinkButton or a Button in its ItemTemplate. In the data-binding expression of the Text property, you should replace the outer double quotes by single quotes to avoid the conflict with the inner double quotes:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="CallHist" Text='<%# GetJobs(Eval("currStatus")) %>' CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField
DataTextField="USERNAME" HeaderText="USERNAME"
NavigateUrl="~/openOrdersDetails.aspx?lookup_id=" SortExpression="USERNAME"
Target="_blank" />
How can I write the code above so the NavigateUrl contains a value grabbed from a control on the page?
Eg,
NavigateUrl="~/openOrdersDetails.aspx?lookup_id=ValueFromControl"
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>
I have a query expression which I am binding to a GridView in Page_Load. The data I want to capture in the SelectedIndexChaned event is in a BoundField defined thus:
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" Visible="False" />
If I set Visible="True", I have no trouble getting this data. Is there a way to hide the ID field and still get the data?
Depends on how you are trying to get the data. If this is an ID field that is unique for each row in the datasource, use DataKeyNames = "ID" in the GridView declaration. Then, in the code behind, whenever you need the ID, you can use the following line:
string ID = GridView1.Rows[GridRowIndex].DataKeys[0].Value.ToString();
You could also convert one of your BoundFields to a TemplateField, and place a HiddenField in it to store the ID. Like so:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="someOtherDataLabel" runat="server" />
<asp:HiddenField ID="IDHiddenField" runat=server />
</ItemTemplate>
</asp:TemplateField>
Then you could use FindControl() in the RowDataBound event of the GridView to store the ID value.
In a grid view i have used paging for that i have used the view state to store datatale to bind it on GridView1_PageIndexChanging event every thing works fine but the problem happens with the first column which is having the checkbox placed in each row .
On navigation all checked check box becomes unchecked how to maintain the state of check box as well.
this is the aaspx code
<Columns>
<asp:TemplateField HeaderText="Select Student">
<ItemTemplate>
<asp:CheckBox id="Chek" runat="server" Text="select" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Enrollment No." DataField="enrollment_no" />
<asp:BoundField HeaderText="Course Name" DataField="course_name"/>
<asp:BoundField HeaderText="Branch Name" DataField="branch_name"/>
<asp:BoundField HeaderText="Email Id" DataField="email" />
<asp:BoundField HeaderText="Mobile" DataField="mobile"/>
<asp:BoundField HeaderText="Name" DataField="first_name"/>
<asp:BoundField HeaderText="Surname" DataField="last_name" />
</Columns>
</asp:GridView>
Viewstate is intended for postbacks to the same page.
To preserve state when navigating to other pages here are 3 options:
Put your checkbox (or simply true/false) in the Session
Use the PreviousPage property
Or use cookies
Summary of option #2
If you have ot post accross pages,
cookies can be used, and also Cross
Page Posting by setting the
PostBackURL property of a button, then
the POST request is directed at the
specified page, and you can get the
values from the PreviousPage property
of the next page.
Example of using option #3, the Session:
//Set
Session["mySessionVariableName"] = myCheckBox;
//Get
CheckBox myCheckBox = (CheckBox)Session["mySessionVariableName"];
I summarized in more detail here and here