将你那个按钮的commandName设置为selected
dataKeyNames 设置成你的数据主键
然后在gridview的 Rowcommand事件里面通过 eCommandArgument获得主键值
然后、、、
<asp:HyperLinkField Text="查看" NavigateUrl="~/searchInfoaspxtype=album&&text=。。。。。。">
你这个直接改成
<asp:HyperLinkField Text="查看" NavigateUrl='~/searchInfoaspxid=<%# Eval("id")>'>
然后在SearchInfoaspx里面,用RequestQueryString["id"]去查找数据库,这样就什么信息都得到了。。。
你在GridView的属性中有个datakey看见没?在那里面填上你想获得值的属性,一般是主键,按钮点击的方法中写GridView1SelectedDataKey,这个就是你想要的值,根据这个值读出其他想要的值
using System;
using SystemData;
using SystemConfiguration;
using SystemCollections;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;
using SystemDataSqlClient;
public partial class Default4 : SystemWebUIPage
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl;
if (!IsPostBack)
{
string sqlstr = "select top 5 from 飞狐工作室";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlconOpen();
mydaFill(myds, "飞狐工作室");
GridView1DataSource = myds;
GridView1DataBind();
for (int i = 0; i <= GridView1RowsCount - 1; i++)
{
DataRowView mydrv = mydsTables["飞狐工作室"]DefaultView[i];
if (ConvertToString(mydrv["员工性别"])Trim() == "True")
{
ddl = (DropDownList)GridView1Rows[i]FindControl("DropDownList1");
ddlSelectedIndex = 0;
}
if (ConvertToString(mydrv["员工性别"])Trim() == "False")
{
ddl = (DropDownList)GridView1Rows[i]FindControl("DropDownList1");
ddlSelectedIndex = 1;
}
}
sqlconClose();
}
}
public SqlDataReader ddlbind()
{
string sqlstr = "select distinct 员工性别 from 飞狐工作室";
sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlconOpen();
return sqlcomExecuteReader();
}
前台主要代码:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="身份z号码" HeaderText="用户ID" SortExpression="身份z号码" />
<asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/>
<asp:TemplateField HeaderText="员工性别">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# ddlbind()%>' DataValueField="员工性别" DataTextField="员工性别">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
int id = intParse(GridView1DataKeys[gvrRowIndex][0]ToString());
-->
int id = intParse(GridView1DataKeys["PK名称"][gvrRowIndex]ToString());
我猜你是参数写错地方了,如果你都是取GridView1DataKeys[gvrRowIndex][0],那就只会一直取第一列的资料了!
以上就是关于gridview控件,怎么点击选中,获取行的数据全部的内容,包括:gridview控件,怎么点击选中,获取行的数据、gridview如何获取当前行的数值、gridview如何通过按钮取得当前行的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)