如题:protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRadioList();
}
}
//绑定radiolist
private void BindRadioList()
{
this.RadioButtonList1.DataSource = ds.Tables[0];
this.RadioButtonList1.DataTextField = "字段1";
this.RadioButtonList1.DataValueField = "字段2";
this.RadioButtonList1.DataBind();
this.RadioButtonList1.SelectedIndex = 0;
}
protected void Button1_Click(object sender, EventArgs e)
{
string text = this.RadioButtonList1.SelectedItem.Text.ToString();
string value = this.RadioButtonList1.SelectedValue.ToString();
}不管选的是哪个项 获取的总是第一条的数据…… 还请高手指点一二~
{
if (!IsPostBack)
{
BindRadioList();
}
}
//绑定radiolist
private void BindRadioList()
{
this.RadioButtonList1.DataSource = ds.Tables[0];
this.RadioButtonList1.DataTextField = "字段1";
this.RadioButtonList1.DataValueField = "字段2";
this.RadioButtonList1.DataBind();
this.RadioButtonList1.SelectedIndex = 0;
}
protected void Button1_Click(object sender, EventArgs e)
{
string text = this.RadioButtonList1.SelectedItem.Text.ToString();
string value = this.RadioButtonList1.SelectedValue.ToString();
}不管选的是哪个项 获取的总是第一条的数据…… 还请高手指点一二~
this.RadioButtonList1.SelectedIndex = 0;
去掉这句话也测试过,还是一样的结果~只有初始加载的时候和重新上传的时候 调用了这个绑定方法~
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadioButtonList1.DataSource = getTable();
RadioButtonList1.DataTextField = "Name";
RadioButtonList1.DataValueField = "ID";
RadioButtonList1.DataBind();
RadioButtonList1.SelectedIndex = 0;
}
}
public DataTable getTable()
{
string[] name = { "张三", "李四", "王五", "赵六" };
DataTable dt = new DataTable("");
dt.Columns.Add("ID", typeof(System.Int32));
dt.Columns.Add("Name", typeof(System.String));
dt.Columns.Add("Number", typeof(System.Double));
for (int i = 0; i < name.Length; i++)
{
DataRow row = dt.NewRow();
row[0] = i + 1;
row[1] = name[i];
row[2] = 12.3;
dt.Rows.Add(row);
}
return dt;
} protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(RadioButtonList1.SelectedItem.Text.ToString());
Response.Write(RadioButtonList1.SelectedValue);
}
没有设置autopostback 为true~ 纠结……
<td colspan="2" style="width:900px">
<asp:Panel ID="pnlExcel" runat="server" ScrollBars="Vertical" Height="100px" BorderStyle="Dotted" BorderColor="Gray">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" >
</asp:RadioButtonList>
</asp:Panel>
</td>
<td><asp:Button ID="Button1" runat="server" Text="选择" Width="75px"
onclick="Button1_Click"/></td>
</tr>
前台页面代码 就是这了~ 真不知道是哪出了问题还是什么原因 就是取不到对应选择的数据……
if (this.selectdatatable().Rows.Count > 0)
{
this.RadioButtonList1.DataSource = GetTabelBySql();
this.RadioButtonList1.DataTextField = "ExcelFile";
this.RadioButtonList1.DataValueField = "ExcelPath";
this.RadioButtonList1.DataBind();
RadioButtonList1.SelectedIndex = 0;
}
很怪异~ 上面这中方法和下面的唯一的区别就是绑定的数据源不一样,上面的是从数据库中查出来的~ 下面的是手动写进去的~ 这种写死的就正常,动态绑定的就不行…… 这怎么也说不通啊~
RadioButtonList1.DataSource = GetTable();
RadioButtonList1.DataTextField = "ExcelFile";
RadioButtonList1.DataValueField = "ExcelPath";
RadioButtonList1.DataBind();
RadioButtonList1.SelectedIndex = 0;