Pass data via hyperlink in gridview - c#

I would like to pass data from a database column to another page via a hyperlink in the gridview, the data i want to pass is just a load of text. this is the code i have
<asp:HyperLinkField DataTextField="FullText"
DataTextFormatString="View Text" NavigateUrl="~/Abstract.aspx"
Target="_blank" />
This appears to work as far as opening up the correct page, but im unsure as to how to view the text on the new page, most help topics tell you how to pass the data to a new grid view, but i would just like to view the data within the page or a box or whatever will work.
Thanks

If my understanding is correct you just want to display the text passed as a query string to the new page, if this is correct, just read the query string and display it in a label.
In order for this to work, you need to specify the query string in the link inside your grid, your link would have to look something like;
~/Abstract.aspx?d=your+text
In your datagrid:
<asp:TemplateColumn>
<ItemTemplate>
<asp:HyperLink
NavigateUrl='<%# "~/Abstract.aspx?d=" + HttpUtility.UrlEncode(DataBinder.Eval(Container, "DataItem.Id").ToString()) %>'
runat="server"
Text="Product" />
</ItemTemplate>
</asp:TemplateColumn>
In the target page you would have something like:
string text = string.Empty;
if (this.Request.QueryString["d"] == null)
text = "Not found";
else
text = Server.UrlDecode(this.Request.QueryString["d"]);
// encode the text to avoid XSS (cross-site scripting)
this.myLabel.Text = Server.HtmlEncode(text);

On the grid
<asp:HyperLinkField DataTextField="FullText"
DataTextFormatString="View Text"
NavigateUrl='<%# "~/Abstract.aspx?ft=" + System.Web.HttpUtility.UrlEncode(Eval("FullText").ToString() %>'
Target="_blank" />
On the page you want to read the param you would have
string fullText = Request.QueryString["ft"];
if (string.IsNullOrEmpty(fullText))
{
fullText = HttpUtility.UrlDecode(fullText);
}

You might want to pass a key parameter through querystring to pull data you want to view.
If that is not what you want please make your question more explanatory.

You can pass ID in session or in query string (Abstract.aspx?textid=1). Read this id in page load event get data from database and display.

Related

Value of HiddenField Duplicated when called from Nested Gridvew Row Editing

Problem: I'm trying to get a value from a HiddenField object and set it to a string. A value of "foo" is being returned as "foo, foo".
Details:
I have a Nested Gridview, and I'm trying to make the child Gridview editable. I have a method GetChildQuery(string id) which form the sql query to get the data for the Child Gridview, and I have the id bound to a HiddenField object in the HTML as so:
<asp:Gridview ID="gvChild" runat="server" AutoGenerateColumns="false" OnRowEditing = "gvChild_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label runat="server" ID="lblNameChild" Text='<% Eval("Name")%>'></asp:Label>
<asp:HiddenField runat="server" ID="hidIDChild" Value ='<%# Bind("ItemID") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" ID="lblNameChildEdit" Text='<% Eval("Name")%>'></asp:Label>
<asp:HiddenField runat="server" ID="hidIDChildEdit" Value ='<%# Bind("ItemID") %>' />
</EditItemTemplate>
</asp:TemplateField>
and I try to access it in the OnRowEditing method like:
protected void gvhild_RowEditing(object sender, GridViewEditEventArgs e) {
Gridview child = sender as Gridview;
string itemID = (child.Rows[e.NewEditIndex].FindControl("hidIDChild") as HiddenField).Value.ToString();
string sql = GetChildQuery(itemID);
child.EditIndex = e.NewEditIndex;
child.DataSource = GetData(sql);
child.DataBind();
}
The id is used in the WHERE clause of my SQL query, and therefore the output is incorrect if the value of the id is incorrect. I don't know I this problem is occurring because of how I bind data to the HiddenField or how I'm calling it from the RowEditing method, or something I'm completely overlooking.
Ref: To make the Nested Gridview, I mainly followed https://www.aspsnippets.com/Articles/Nested-GridView-Example-in-ASPNet-using-C-and-VBNet.aspx and https://www.aspforums.net/Threads/133072/Edit-Update-Delete-in-Nested-Child-GridView-in-ASPNet/ which has the general format for my GetData method as well.
EDIT: Problem subverted by replacing the string itemID instantiation to
string itemID= ((child.Rows[e.NewEditIndex].FindControl("hidIDChild") as HiddenField).Value.Split(','))[0].ToString();
essentially splitting the duplication, accessing the first element, converting that to a string, and setting that equal to y string itemID.
However while this subverts the problem, I would still appreciate anyone's feedback as to why the problem is occurring in the first place. Thanks!
You should look at your rendered html.
When dealing with one gridview there is only ever one row open for editing at a time. And within a row's TemplateField the ItemTemplate and EditTemplate are mutually exclusive, that is to say that, when rendered, you will see only one or the other.
Likewise in the code behind, you will only have access to either the ItemTemplate row or the EditTemplate row depending on which event you process on postback.
The same rules apply to a nested/child Gridview.
So for the case you describe, I assume you are trying to use some ID from the parent to build a query to populate the child. If so I do not believe you need any hidden fields at all. This ID fieldname from the parents query should be set in the DataKeyNames property of the parent gridview.
You should then be able to pass the value to the procedure you are building for the child as
protected void gvhild_RowEditing(object sender, GridViewEditEventArgs e) {
string sql = GetChildQuery(gvParent.SelectedDataKey.Value);
child.DataSource = GetData(sql);
child.DataBind();
// Given that you have just populated the child I do not think the
// following is accurate, but that's for you to decide
child.SetEditRow(e.NewEditIndex);
}
Hope this helps.

google map parameter not showing

I have textbox in asp.net where i am writting Address.
On linkbutton below i wanted to direct it to goole map.
I have below textbox:
<asp:TextBox ID="txtJobAddress" runat="server" TextMode="MultiLine" Width="95%" Height="20px"
onBlur="javascript:saveChanges('JobAddress');"></asp:TextBox>
Linkbutton as below:
<a href="http://maps.google.com/maps?q=<%=txtJobAddress.Text%>" target="_blank">
<img src="images/gmap_button.gif" alt="Map" />
</a>
but when its getting directed through anchor tag, its not catching "q" parameter.
Its giving as:
https://www.google.com/maps/preview?q=
why its not taking value of:
<%=txtJobAddress.Text%>
Please help me , how can i attach textbox string here in
http://maps.google.com/maps?q=<%=txtJobAddress.Text%>
as below
http://maps.google.com/maps?q=NewYork
The <%=txtJobAddress.Text%> will be empty on page load. Presumably your saveChanges method prompts an ajax call to save the "JobAddress"? If this is the case then a full page postback won't occur, and therefore the <%=txtJobAddress.Text%> isn't populated with the updated value as this would occur on the server. You'll want to populate the query string value client side
e.g. in your blur event you could add a call to a javascript function that updates your link:
onBlur="javascript:saveChanges('JobAddress'); updateLink()"
in your updateLink function you would update the link query string value

Passing Query String parameter to a DataNavigateUrl Format String

I am trying to alter an existing search page to also allow a user to find comparable records based on a query string. I fist have them select a parent and then reload the same existing search page with an added paramter to the URL "PID". Using this when I run the search again I want to be able to select a child record ID then go to a new page to compare. However I cannot get the oringal PID from the query string in the datanavigateUrlFormatString
<asp:HyperLinkField DataNavigateUrlFields="ID, "
DataNavigateUrlFormatString="~\Contributor\Search.aspx?LinkWizard=true&CID={0}&PID=" HeaderText="Select Child"
Text="Set Child" />
Essentially I need the PID to get pulled from the query string. How is this done, it seems it should have been simple but I can find nothing that works for me. Thanks.
I'm not sure where you're using the HyperLinkField but can you change it to a TemplateField and use Eval? Something like this:
<asp:TemplateField HeaderText="Select Child">
<ItemTemplate>
<asp:HyperLink runat="server" NavigateUrl='<%#Eval("ID", "~/Contributor/Search.aspx?LinkWizard=true&CID={0}&PID=") + Request.QueryString["PID"]%>' Text="Set Child" />
</ItemTemplate>
</asp:TemplateField>

How to select a row from grid view and edit the details in a new webform using ASP.NET

I have displayed some fields in grid view, Now i want to select a row and i want to display all field from sql server in to a web form, after displaying the data in respective controls(textbox, dropdown), i want to update it.
How i can redirect the page to new webpage after selecting the respective row from grid view (Using primary key). and how the data will be pass on page load in respective fields so i could able to update it by using update query.
As i new to ASP. Net. Please update me in detail. If possible please with code.
It would go something like this.
You will make a 'list' webform with all records containing the gridview
Make another webform called details
The gridview on list page will contain a hyperlink button field something like this
<asp:HyperLinkField Text="View Details" DataNavigateUrlFields="YourId" DataNavigateUrlFormatString="details.aspx?id={0}" />
(Notice I am just redirecting to the details page passing the field id with querystring.
Now, on details page you would just fetch the querystring value & query your database with a datareader & populate your details page.
Something like on page load
if(Request.QueryString["id"])!=null)
{
// Fetch respective item from database here & populate form fields
// Open connection(); execute datareader;populate form fields
}
Easiest way to do this:
<asp:GridView ID="grd" runat="server" autogeneratedcolumn="false">
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<%#Eval("ID")%>
<ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("ID")%>
<ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HaderText="Edit">
<ItemTemplate>
<a href='EditData.aspx?ID=<%#Eval("ID")%>'>Edit</a>
</ItemTemplate>
<asp:TemplateField>
<asp:GridView>
EditData.aspx page will be used to edit the data. After receiving the Query-string Value,we can bind the data to the controls and then update the data after making changes.

passing dynamic values for each row in listview

i am using a list view for viewing some information to user there i used a hyperlink in each row to show detailed information in a modal popup in the same page what i want on the click of particular hyperlink the detailed report of that row only should display.
[ID] is the primary key in my database iam passing that as an object from aspx file to the code behind and using that id to fetch data from database.
but each time it is showing same information.
<asp:HyperLink ID="hlnkShow" runat="server" CssClass="showButton" OnClick='<%# Attach(Eval("ID")) %>' Text="Show" ToolTip="View the contents of the message"> </asp:HyperLink>
in the code behind in the attach function
protected string Attach(object ObjectID)
{
return ("return showComments($(this),'" + ObjectID + "');");
}
in the aspx again in the javascript
function showComments($element, objectID)
{
$('input[type=hidden][id$=hfObjectID]').val(objectID);
}
and using this hiddenfield value iam making a ajax call and the problem is solved

Categories