问题描述:

I have here the link of tutorial about the Grid view Selection row.

ASPLINK

I did the same thing based on the tutorial, I tried to understand the concept and it works. But I want to know how to display the selected row through radio button in every labels that I've created such as; Customer ID, City and Postal

And I've Created also a button to submit or display the selected row. But I don't know how to code it with VB.net

ASP:

<asp:GridView ID="GridView1" runat="server"

HeaderStyle-BackColor = "green"

AutoGenerateColumns = "false" Font-Names = "Arial"

OnPageIndexChanging = "OnPaging"

Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"

AllowPaging = "true">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:RadioButton ID="RadioButton1" runat="server"

onclick = "RadioCheck(this);"/>

<asp:HiddenField ID="HiddenField1" runat="server"

Value = '<%#Eval("CustomerID")%>' />

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField ItemStyle-Width="150px" DataField="CustomerID"

HeaderText="CustomerID" />

<asp:BoundField ItemStyle-Width="150px" DataField="City"

HeaderText="City" />

<asp:BoundField ItemStyle-Width="150px" DataField="PostalCode"

HeaderText="PostalCode"/>

</Columns>

</asp:GridView>

<asp:Button ID="Select" runat="server" Text="Select" />

VB.NET

 Private Sub BindGrid()

OpenConnection()

str = "SELECT CustomerID, City, PostalCode FROM Customers"

adapt = New SqlDataAdapter(str, con)

adapt.Fill(dt)

GridView1.DataSource = dt

GridView1.DataBind()

adapt.Dispose()

con.Close()

End Sub

Private Sub GetSelectedRecord()

For i As Integer = 0 To GridView1.Rows.Count - 1

Dim rb As RadioButton = DirectCast(GridView1.Rows(i).Cells(0).FindControl("RadioButton1"), RadioButton)

If rb IsNot Nothing Then

If rb.Checked Then

Dim hf As HiddenField = DirectCast(GridView1.Rows(i).Cells(0).FindControl("HiddenField1"), HiddenField)

If hf IsNot Nothing Then

ViewState("SelectedContact") = hf.Value

End If

Exit For

End If

End If

Next

End Sub

Private Sub SetSelectedRecord()

For i As Integer = 0 To GridView1.Rows.Count - 1

Dim rb As RadioButton = DirectCast(GridView1.Rows(i).Cells(0).FindControl("RadioButton1"), RadioButton)

If rb IsNot Nothing Then

Dim hf As HiddenField = DirectCast(GridView1.Rows(i).Cells(0).FindControl("HiddenField1"), HiddenField)

If hf IsNot Nothing And ViewState("SelectedContact") IsNot Nothing Then

If hf.Value.Equals(ViewState("SelectedContact").ToString()) Then

rb.Checked = True

Exit For

End If

End If

End If

Next

End Sub

Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

GridView1.PageIndex = e.NewPageIndex

GridView1.DataBind()

SetSelectedRecord()

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

GetSelectedRecord()

BindGrid()

End Sub

Javascript: The function of this code is to make sure that only one RadioButton is checked

<script type = "text/javascript">

function RadioCheck(rb) {

var gv = document.getElementById("<%=GridView1.ClientID%>");

var rbs = gv.getElementsByTagName("input");

var row = rb.parentNode.parentNode;

for (var i = 0; i < rbs.length; i++) {

if (rbs[i].type == "radio") {

if (rbs[i].checked && rbs[i] != rb) {

rbs[i].checked = false;

break;

}

}

}

}

</script>

Please Help me Guys!

Thanks in Advanced :)

相关阅读:
Top