这是我在做毕业设计的时候遇到的一个问题然后从网上查到的结果,就三行代码
有时我们要对GridView进行其它操作,这时就需要得到行号:
前台:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Update"
Text="认证" OnClick="LinkButton2_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True"
SortExpression="Id" />
<asp:BoundField DataField="AdminName" HeaderText="帐号" SortExpression="AdminName" />
<asp:TemplateField HeaderText="验证" SortExpression="isPass">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("isPass") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# IsPass(Eval("isPass").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PwdQussion" HeaderText="密码问题" SortExpression="PwdQussion" />
<asp:BoundField DataField="PwdAnsow" HeaderText="问题答案" SortExpression="PwdAnsow" />
<asp:BoundField DataField="Sex" HeaderText="性别" SortExpression="Sex" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="CompyWeb" HeaderText="公司网址" SortExpression="CompyWeb" />
<asp:BoundField DataField="CompyName" HeaderText="公司名称" SortExpression="CompyName" />
<asp:BoundField DataField="Adress" HeaderText="收货地址" SortExpression="Adress" />
<asp:BoundField DataField="AdressMan" HeaderText="收货人" SortExpression="AdressMan" />
<asp:BoundField DataField="Code" HeaderText="邮政编码" SortExpression="Code" />
<asp:BoundField DataField="Phone" HeaderText="联系电话" SortExpression="Phone" />
<asp:BoundField DataField="MovePhone" HeaderText="手机" SortExpression="MovePhone" />
<asp:BoundField DataField="Fax" HeaderText="传真" SortExpression="Fax" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
后台:
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow gvr = (GridViewRow)lb.Parent.Parent;
Label pass = (Label)GridView1.Rows[gvr.RowIndex].Cells[2].FindControl("Label1");
if (pass.Text == "否")
{
SqlDataSource1.UpdateParameters["isPass"].DefaultValue = "1";
}
else
{
SqlDataSource1.UpdateParameters["isPass"].DefaultValue = "0";
}
}
|
|