我这里有关于网格视图选择行的教程链接。
我根据教程做了同样的事情,我试图理解这个概念并且它有效。但我想知道如何在我创建的每个标签中通过单选按钮显示所选行,例如;客户ID,城市和邮政
我还创建了一个提交或显示所选行的按钮。但我不知道如何使用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:此代码的功能是确保只检查一个RadioButton
<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>
请帮帮我吧!
感谢高级:)