Passing Query String parameter to a DataNavigateUrl Format String - c#

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>

Related

Recording clicked Grid View column

I have a grid view in a asp page which have a hyperlink field,say,CustomerID which navigates user to various webpages. The value of the CustomerID in the grid view can have duplicate values. So, what I want is how to record all the clicked values in the CustomerID or the hyperlink?
Please suggest solution such that the solution is viable for multiple hyperlink column in gridview each navigation to different webpage.
You can use combination of gridview bind columns and pass them as parameters in url and retrieve in other page as Request.QueryString[] collection.
Eg
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" NavigateUrl='<%# string.Format("~/Details.aspx?CustomerID={0}&Name={1}&Country={2}",
HttpUtility.UrlEncode(Eval("CustomerID").ToString()), HttpUtility.UrlEncode(Eval("Name").ToString()), HttpUtility.UrlEncode(Eval("Country").ToString())) %>'
Text="View Details" />
</ItemTemplate>
</asp:TemplateField>

Values GridView in cells

I am trying to get values of string that I keep inside GridView.
When I use regular BoundFields, things work get. I get whatever is needed with:
string my_value = myGrid.Rows [rowIndex].Cells[1].Text;
However, one grid needs to have hyperlinked entries in one of the columns. I did:
<asp:BoundField DataField="domainName"
HeaderText="Domain"
SortExpression="domainName"
HtmlEncode="false"
DataFormatString="<a href=DomainConfiguration.aspx?suffix={0}>{0}</a>"
My ASPX page shows the correctly formed hyperlinks. However, if I retrieve Text for the cell, it returns "<\a href=DomainConfiguration.aspx?suffix=example.com>example.com</a>" [without the two extra slashes], instead of "example.com"
What do I need to do to get GridView working the way I want? [Yes, I would rather use GridView and not another control.]
Thank you.
Instead of asp:BoundField to show hype link it is better use asp:HyperLinkField like:
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="domainName" HeaderText="domainName" DataNavigateUrlFormatString="DomainConfiguration.aspx?suffix={0}" DataTextField="domainName" />
</Columns>
and to get string value of that column you should try this:
string my_value= ((HyperLink)myGrid.Rows[rowIndex].Cells[1].Controls[0]).Text;
instead your asp:Bounfield use
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="editLink" runat="server" onclick='<%#Eval("EditLink") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Error when displaying data in a datalist from sqldatasource in asp project

I am building a website for a class and have come across an error that I can not find the solution to. I have a dropdownlist on the page that displays the cutomer name and sets the selected value to the customer id selected from a sqldatasource (data tables from access database - fig1) (this works fine as I have tested it before I added the second sqldatasource). I add a datalist control to the page and a second sqldatasource for the datalist to bind to. I configured the datasource using the configure data source wizard as shown in fig2. I then use the wizard to test the data returned by the query and see that it works, the wizard shows that the data that I wanted is returned. So I bind the datalist to the data source and now the itemtemplate (in the html source view) contains data bound labels that will show the values:
Products.Name:
<asp:Label ID="Products_NameLabel" runat="server" Text='<%# Eval("Products.Name") %>' />
<br />
Technicians.Name:
<asp:Label ID="Technicians_NameLabel" runat="server" Text='<%# Eval("Technicians.Name") %>' />
<br />
Incidents.DateOpened:
<asp:Label ID="Incidents_DateOpenedLabel" runat="server" Text='<%# Eval("Incidents.DateOpened") %>' />
<br />
Incidents.DateClosed:
<asp:Label ID="Incidents_DateClosedLabel" runat="server" Text='<%# Eval("Incidents.DateClosed") %>' />
<br />
Incidents.Description:
<asp:Label ID="Incidents_DescriptionLabel" runat="server" Text='<%# Eval("Incidents.Description") %>' />
The labels are now bound to the data returned from the second source....or so it would seem. When I run it and select a customer that should return the data that is being selected in the sqldatasource select statement, my program crashes and gives this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Products'.
I do not understand why it is saying that the datarowview does not contain a column with that name when the sqldatasource clearly returns it when I test it in the wizard. Can anyone please help?
P.S. Sorry for the links to imgur...apparently you have to have a higher rep to post pictures
I tried this and the generated HTML was NOT prefixed with the table name like in your code.
I hand-added the table name and reproduced your error.
So, if you have duplicate column names like you do, you need to modify the SQL statement to use an alias:
Products.Name as Products_Name

Pass data via hyperlink in gridview

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.

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.

Categories