How to add a hyperlink to my gridview - asp.net

I want to set hyperlink (on image) in my gridview. When user clicks on that hyperlink, a query string should be generated based on selected value of dropdown list. How to set the hyperlink in gridview and how to form query string for that hyperlink?
Thanks in advance..

You can simply Cancatinate the value of your dropdown to NavigateUrl property of hyperlink
<ItemTemplate>
<asp:HyperLink ID="hlEdit" runat="server"
NavigateUrl='<%# Eval("ID", "PageName.aspx?ID={0}" + "&TID=" + ddl.SelectedValue) %>'
ImageUrl="~/Images/edit.png"></asp:HyperLink>
</ItemTemplate>
Edit:
<ItemTemplate>
<asp:ImageButton ID="hlEdit" runat="server"
PostBackUrl='<%# Eval("ID", "PageName.aspx?ID={0}" + "&TID=" + ddl.SelectedValue) %>'
ImageUrl="~/Images/edit.png"></asp:ImageButton>
</ItemTemplate>

You will probably need javascript for this.
Add an 'onclick' attribute to your images
In the onclick handler, you retrieve the dropdownlist value and compose your query
Set the composed url to the href of your link
Some more detailed information would be useful to be able to provide you with some code..
Are you using an asp HyperLink, ImageButton, ...?
You could for example use the OnClientClick property in case you would be using an ImageButton.

Related

Navigate URL for Hyperlink is changeed, when the Title set to hyperlink has "/" inbetween?

I am using hyperlinks inside datalist and the datalist is binded with a datatable
Hyperlink inside my datalist :
<asp:HyperLink ID="hypSubSections" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"Pagename") + "?ID=" + DataBinder.Eval(Container.DataItem,"ID") + "&Code=" + DataBinder.Eval(Container.DataItem,"CODE") + "&Title=" + DataBinder.Eval(Container.DataItem,"Title") %>' Text='<%# DataBinder.Eval(Container.DataItem,"Title") %>'></asp:HyperLink>
On Page load all the hyperlinks have correct navigate url set, When there is a case where the TItle set to hyperlink is "Criteria/Admission" and now I click that hyperlink, the Pagename value set to hyperlink is changed somehow and to all other hyperlinks from there..
The problem arises only when the text has slash inbetween. How to handle this ? It looks weird to me.
try this
Text='<%# Server.HtmlEncode(DataBinder.Eval(Container.DataItem,"Title")) %>'
Update:
how about setting the text not by attribute?
<asp:HyperLink ID="hypSubSections" runat="server"><%# Server.HtmlEncode(DataBinder.Eval(Container.DataItem,"Title")) %></asp:HyperLink>

Using hyperlink with querystring for gridview row

Is there someway to turn the row of a gridview into a hyperlink so that when a user opens it in a new tab for example, it goes to that link? Right now I am using a LinkButton and when the user opens it in a new tab, it doesn't know where to go.
I figured the .aspx code would look something like:
<asp:TemplateField>
<ItemTemplate>
<Hyperlink ID="hyperlink" runat="server" ForeColor="red" HtmlEncode="false" navigationURL="testUrl.aspx"
</ItemTemplate>
</asp:TemplateField>
The only thing is, our URLs are set up in the C# code behind as a query string, so I'm not sure how to pass that into the navigationURL section.
I'm guessing there's something I can do on the page_load with the query string to redirect to the page I need, but this is my first time working with query strings so I'm a little confused.
Thanks!
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#String.Format("~/controller.aspx?routeID1={0}&routeID2={1}", Eval("routeid1"), Eval("routeid2"))%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
routeid1 and routeid2 are passed as query strings to the controller of that page.
What I did recently is modified my class to have a readonly property that constructs the A tag for me. This way I have control over what gets displayed; just text or a link.
<ItemTemplate>
<asp:Label ID="ColumnItem_Title" runat="server" Text='<%# Bind("DownloadATag") %>'> </asp:Label>
</ItemTemplate>
The code behind just binds an instance of the class to the gridview. You can bind the gridview whenever, on load on postback event, etc.
Dim docs As DocViewList = GetViewList()
GridViewDocuments.DataSource = docs
GridViewDocuments.DataBind()
In the above code, the DocViewList, instantiated as docs, is a list of a class that has all the properties that are needed to fill my GridView, which is named GridViewDocuments here. Once you set the DataSource of your GridView, you can bind any of the source's properties to an item.
Something like:
<asp:LinkButton ID="LinkButton_Title" runat="server" target="_blank"
PostBackUrl='<%# Eval(Request.QueryString["title"]) %>'
or binding them from the RowCreated event:
protected void GridView_OnRowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.FindControl("LinkButton_Title") as LinkButton).PostBackUrl = Request.QueryString["title"]))
}
}

hyperlink in a item template inside the gridview

I have this hyperlink in a item template inside the gridview
<asp:TemplateField Headertext ="SN0">
<ItemTemplate>
<asp:Hyperlink runat= "server" Text='<%# DataBinder.Eval(Container.DataItem,"Container.DataItemIndex + 1")%>'
NavigateUrl='<%# "ResolveComplaint.aspx?Name=" & DataBinder.Eval (Container.DataItem,"ComplaintProfileId").tostring & _
"&Status=" & DataBinder.Eval(Container.DataItem,"Status").tostusring %>' ID="Hyperlink2"/>
</ItemTemplate>
</asp:TemplateField>
Basically, I am trying to make the first column(SN0) in a gridview. On click on the hyperlink, it redirects to another Page. I am carrying ComplaintProfileId, Status fields
to the next page
This gives me a compiletime error:
Compiler Error Message: CS1026: ) expected
Thanks
Sun
The problem is when you are trying to set the NavigateUrl property. You are using the & for concatenation, but you have to use the + sign for that. e.g.
NavigateUrl='<%# "ResolveComplaint.aspx?Name=" + DataBinder.Eval (Container.DataItem,"ComplaintProfileId").tostring +
"&Status=" + DataBinder.Eval(Container.DataItem,"Status").tostusring %>'
Should your .tostring and tostusring calls be .ToString()?

FindControl("someTextBox") in GridView not sending the updated value

Im populating a GridView from List so am forced to use TemplateField controls to allow editing. This requires displaying a TextBox populated with the original value when in edit mode and using FindControl to get the new value out on update submit.
Problem is foundTextBox.Text == "OriginalTextBoxValue"
<asp:TemplateField HeaderText="A Field">
<ItemTemplate>
<asp:Label ID="_theLabel" runat="server" Text='<%# Eval("AField") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_theTextBox" runat="server" Text='<%# Eval("AField") %>' />
</EditItemTemplate>
</asp:TemplateField>
And the code in my update event handler
TextBox newText = (TextBox)_myGridView.Rows[e.RowIndex].FindControl("_thTextBox");
//newText.Text == the old value of the text box
Is your gridview binded at every postback? This could explain why you never get the updated value, because the gridview is rebinded before reading the textbox.
Could you paste your complete update method?
You've got the code behind in the wrong event handler. Move it to the Editing event handler, so it will populate the textbox whenever the user clicks on the Edit command for a row.

asp.net HyperLinkField Has no ToolTip property (Alt text)

I wish there was a ToolTip field in HyperLinkField as there is one in HyperLink.
I'm creating a HyperLinkField by code before binding to my data source:
HyperLinkField hl = new HyperLinkField();
hl.DataNavigateUrlFields = new string[] { "col" };
hl.DataNavigateUrlFormatString = "{0}";
hl.DataTextField = "Foo";
Is there any way to also set a value to something which will render as a tooltip (or alt text)?
Any help will be appreciated.
That's correct, there's no tooltip/alt text property in a HyperlinkField. To get around this shortcoming, you need to use a template field and add a regular Hyperlink control.
<asp:TemplateField HeaderText="Href">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#
Eval("Href") %>' Text='<%# Eval("Href") %>' ToolTip='<%# Eval("Text") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
However, doing this in a programmatic requires a lot of work. You need to create your own class that implements the ITemplate interface. Here's a tutorial on that.
Your requirement can be accompished in <asp:HyperlinkField> itself by adding tooltip for that specific cell in RowDataBound event of a GridView. After binding the GridView to your DataSource you can do this in a RowDataBound event as follows:
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].ToolTip = "Your tooltip text";
}
Though you have accepted another answer, my answer may be helpful for some other users!

Resources