Pass values from QueryString parameter in GridView hyperlink - asp.net

I'm using GridView with a HyperLink column, and I want to do the following:
<asp:HyperLinkField DataNavigateUrlFields="DID"
DataNavigateUrlFormatString="~/student/group/document/Body.aspx?DID={0}&GN={QueryString("GN")}" HeaderText="View Document" Text="view" />
How I can retrieve the value of GN from the QueryString parameter and add it to the HyperLink column ?

How important is it to you to do this in the markup? I'm fairly sure you can't do this in the markup with the DataNavigateUrlFields and DataNavigateUrlFormatString, but you can do it in code in the RowDataBound event:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim link As HyperLink
Dim row As DataRow
If e.Row.RowType = DataControlRowType.DataRow Then
'Get the row we are binding to
row = CType(e.Row.DataItem, DataRowView).Row
'Find the hyperlink control we want to set the link for
link = CType(e.Row.Controls(1).Controls(0), HyperLink)
'Check if the querystring we want to include is missing or empty
If Request.QueryString("GN") Is Nothing Or Request.QueryString("GN") Is String.Empty Then
'If there is no querystring then we can only bind to the DID
link.NavigateUrl = "~/student/group/document/Body.aspx?DID=" & row.Item("DID").ToString
Else
'The querystring element is present so include it in the link
link.NavigateUrl = "~/student/group/document/Body.aspx?DID=" & row.Item("DID").ToString & "&GN=" & Request.QueryString("GN").ToString
End If
End If
End Sub

Related

How to get the ID of a editing textbox in gridview

I want to get the ID of a textbox like below to add validator, the Client ID contains generated string, UniqueID too, but only the ID contains nothing, why?
Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'Manipulate only editing row.
If e.Row.RowType = DataControlRowType.DataRow Then
If sender.EditIndex = e.Row.RowIndex Then
'Search textbox and add validators.
For Each cell As TableCell In e.Row.Cells
If cell.Controls.Count = 1 AndAlso TypeOf (cell.Controls(0)) Is TextBox Then
Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)
'txt.ID is nothing...why?
SetValidators(cell.Controls, txt.ID)
End If
Next
End If
End If
End Sub
Well here is a workaround if applies to your application ,
Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)
//'txt.ID is nothing...why?
// Here you can assign new ID to your control as per your logic
txt.ID = "newID";
SetValidators(cell.Controls, txt.ID)
You can try following code to assign the validator control to the gridview control.
I am not sure what your doing with SetValidators()function.
Protected Sub GridView1_RowDataBound(ByVal sender As GridView, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'Manipulate only editing row.
If e.Row.RowType = DataControlRowType.DataRow Then
If sender.EditIndex = e.Row.RowIndex Then
'Search textbox and add validators.
For Each cell As TableCell In e.Row.Cells
If cell.Controls.Count = 1 AndAlso TypeOf (cell.Controls(0)) Is TextBox Then
Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)
'txt.ID is nothing...why?
SetValidators(cell.Controls, txt.ClientID)
End If
Next
End If
End If
End Sub
Finally, I'v solved that problem by to apply the workaround below.
Dim txt As TextBox = DirectCast(cell.Controls(0), System.Web.UI.WebControls.TextBox)
'The ID is generated by to refer ClientID.
Dim foo = txt.ClientID
'Therefore, already txt.ID is not nothing.
SetValidators(cell.Controls, txt.ID)
Thank you for your cooperation.

setting radiobuttonlist value within a gridview using code behind

I am trying to set the selected value of a radiobuttonlist that is a template column within a gridview. I am having to do this using the code behind as the database field containing the data (Status) contains null values hence I cant used SelectedValue='<%# Bind("Status") %>' on the asp side.
It has been suggested that I use onRowDataBound to do this and use DataItem to retrieve the value from the datasource and used it to set the radiobuttonlist selected value but I'm not sure how to do this in vb code.
I've tried the following:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim radioList = TryCast(e.Row.FindControl("rblActions"), RadioButtonList)
' this will be the object that you are binding to the grid
Dim myObject = TryCast(e.Row.DataItem, DataRowView)
Dim sStatus As String = Convert.ToString(myObject("Status"))
If sStatus <> Nothing Then
radioList.SelectedValue = sStatus
End If
End If
End Sub
but it hasnt work. Any help would be appreciated. Thanks
Found my own solution as follows:
Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rbl As RadioButtonList = CType(e.Row.FindControl("rblActions"), RadioButtonList)
Dim selected As String = e.Row.DataItem("Status").ToString
If Not IsNothing(selected) Then
rbl.SelectedValue = selected
End If
End If
End Sub

Page_PreRender finding div by id (asp.net VB)

I am trying to run some code on Page_PreRender but only want it to run on hyperlinks within a certain DIV.
What the code does is change the colour of a hyperlink if the NavigateUrl = the URL of the page the user is on.
I have some code that works but it changes the colour of every link on the page that matches when I only want it to happen within a certain div.
The DIV ID i want the hyperlinks changed in is 'subNav'
CURRENT CODE
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path
Dim strControlType As String
For Each ctrl As Control In Me.Controls
For Each subctrl As Control In ctrl.Controls
strControlType = Convert.ToString(subctrl.[GetType]())
If strControlType = "System.Web.UI.WebControls.HyperLink" Then
If filePath = "/" & DirectCast(subctrl, HyperLink).NavigateUrl Then
'DirectCast(subctrl, HyperLink).CssClass = "active"
DirectCast(subctrl, HyperLink).Attributes.Add("style", "color:#993366")
'Label2.Text = "/" & DirectCast(subctrl, HyperLink).NavigateUrl
End If
End If
Next
Next
End Sub
CODE IM TRYING
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path
Dim strControlType As String
Dim subNavDiv As HtmlControl = CType(FindControl("subNav"), HtmlControl)
For Each ctrl As Control In subNavDiv.Controls
For Each subctrl As Control In ctrl.Controls
strControlType = Convert.ToString(subctrl.[GetType]())
If strControlType = "System.Web.UI.WebControls.HyperLink" Then
If filePath = "/" & DirectCast(subctrl, HyperLink).NavigateUrl Then
'DirectCast(subctrl, HyperLink).CssClass = "active"
DirectCast(subctrl, HyperLink).Attributes.Add("style", "color:#993366")
'Label2.Text = "/" & DirectCast(subctrl, HyperLink).NavigateUrl
End If
End If
Next
Next
End Sub
Not sure if this is the way to go about it or not, but it doesn't seem to be working.
Thanks for any help.
J.
You will need to add a runat="server" tag to the div and give it an ID. Once you do that, you can find the DIV like this:
EDIT: Use Panel instead of DIV, and add HyperLink controls to the Panel, like this:
<asp:Panel ID="pnlLinks" runat="server">
<asp:HyperLink ID="lnk1" runat="server" Text="Link 1" />
<asp:HyperLink ID="lnk2" runat="server" Text="Link 2" />
</asp:Panel>
Then in your code behind, do this:
For Each lnk As HyperLink In pnlLinks.Controls.OfType(Of HyperLink)()
lnk.NavigateUrl = "/somefolder/somepage.aspx"
Next
UPDATE
I added in some code when iterating through the links:
Response.Write(DirectCast(subctrl, HyperLink).NavigateUrl & "<br />")
But when I added runat="server" to the div the hyperlinks I within the div were no longer writen out.
UPDATE2
Got there with your help, the panel bit definitely worked, thanks.
Final Code:
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path
For Each lnk As HyperLink In subNav.Controls.OfType(Of HyperLink)()
If filePath = "/" & lnk.NavigateUrl Then
DirectCast(lnk, HyperLink).CssClass = "active"
End If
Next
End Sub

Referencing items within a gridview

I have a a gridview attached to an objectdatasource. In each row I have a bound textbox for input. I have a button beside the textbox in each row that launches a javascript popup for unit conversion.
The question is: how can i tell the unit converter (js function) what textbox (in which row) to populate the results with?
In the RowCreated event of the GridView:
Protected Sub MyGridView_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btn As Button = e.Row.Cells(0).FindControl("btnJavaScriptButton")
Dim txt As TextBox = e.Row.Cells(1).FindControl("txtResults")
btn.OnClientClick = "calculate(" & txt.ClientID & ");"
End If
End Sub
Where 0 and 1 are the indices of the columns that contain the button and the textbox, and "calculate" is the name of your JavaScript function.

Rowdatabound of gridview

I am using following code in rowdatabound function.
Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Footer Then
Dim ddlItem As DropDownList = CType(e.Row.FindControl("ddlFProjectLevels"), DropDownList)
If ddlItem IsNot Nothing Then
ddlItem.DataSource = objMileStone.GetProjectLevels()
ddlItem.DataValueField = "MileStoneID"
ddlItem.DataTextField = "Name"
ddlItem.DataBind()
End If
End If
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowState = DataControlRowState.Edit Then
Dim ddlEProjectLevels As DropDownList = CType(e.Row.FindControl("ddlEProjectLevels"), DropDownList)
ddlEProjectLevels.DataSource = objMileStone.GetProjectLevels()
ddlEProjectLevels.DataValueField = "MileStoneID"
ddlEProjectLevels.DataTextField = "Name"
ddlEProjectLevels.DataBind()
ddlEProjectLevels.SelectedValue = milestoneid
End If
End If
End Sub
ddlEProjectLevels is dropdownlist in edititemtemplate. When I click edit in 1st row ddlEProjectLevels gets loaded with data from database. But in 2nd row dropdownlist does not contain values. Again in 3rd it gets loaded from db. Means in alternate rows, when I click edit dropdownlist(ddlEProjectLevels) doesn't load values. Can anybody help?
This issue is somewhat confusing compared to other controls and applies to GridView and DetailsView controls. You'll need to check the RowState enumeration using bitwise logic since an item may have a state of Alternate or Edit.
So instead of:
If e.Row.RowState = DataControlRowState.Edit Then
Try:
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
For more info check out the DataControlRowState Enumeration page.

Resources