asp.net笔记

if (e.Row.RowType == DataControlRowType.DataRow)

        {

            RadioButton rd = (RadioButton)e.Row.Cells[2].FindControl("rd_productID");

            if (rd != null)

            {

                rd.ID = ((System.Data.Common.DbDataRecord)e.Row.DataItem).GetValue(0).ToString();

            }

        }

在GridView中给RadioButton设置值(数据源为SqlDataReader)时应注意两个地方:

1、      RadioButton在客户端的Value值是在服务器端是通过ID属性设置的。

2、      数据源为SqlDataReader时e.Row.DataItem应强制转换成System.Data.Common.DbDataRecord,并且使用GetValue(0)方法指定列来获取值。

SqlParameter 构造函数 (String, Object)

请小心使用 SqlParameter 构造函数的这个重载来指定整数参数值。因为此重载接受 Object 类型的 value,所以当此值为零时,必须将整数值转换为 Object 类型,如下面的 C# 示例所示。

Parameter = new SqlParameter("@pname", Convert.ToInt32(0));

如果不执行该转换,则编译器将认为您尝试调用 SqlParameter(string、 SqlDbType)构造函数重载。

在GridView中设置DataKeyNames,以逗号分隔,索引分别从0开始,

 <asp:GridView ID="gv_product" DataKeyNames="ctrid,pid">

取得键所在行对应字段的值:GridViewID.DataKeys[所在行的索引].Values[键索引]

Repeater控件绑定数据,数据源为SqlDataReader,在ItemDataBound事件中通过

System.Data.Common.DbDataRecord rd = (System.Data.Common.DbDataRecord)e.Item.DataItem;

rd["字段名"].ToString()取得具体字段的字符串值。

原文地址:https://www.cnblogs.com/xueyuan299/p/1282929.html