【问题标题】:Populate radiobuttonlist with yes or no options, and save to database on clicking使用是或否选项填充单选按钮列表,并在单击时保存到数据库
【发布时间】:2012-10-05 16:54:25
【问题描述】:

C# 代码。我之前填充了复选框,但没有填充单选按钮。 这是一个示例页面:

<asp:Content ID="Content" runat="server" ClientIDMode="Static">
    <asp:UpdatePanel ID="category" runat="server">
        <ContentTemplate>
            <div>
                <span>Data:</span>
                <asp:TextBox ID="txBox" runat="server" TextMode="MultiLine">
                </asp:TextBox>
            </div> 
            <!-- Need to add this to populate from dataset, and save to database table -->     
            <asp:RadioButtonList ID ="radioButtonList" runat="server" AutoPostBack="true"
                OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"  >
                <asp:ListItem ID="isEnabledTrue">Yes</asp:ListItem>
                <asp:ListItem ID ="isEnabledFalse" Selected="True">No</asp:ListItem>           
            </asp:RadioButtonList>    
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

我如何以同样的方式填充 radioButtonList。 返回的表/数据集为 名称 : NVARCHAR(25) 已启用:1/0 - 位

如何将“isEnabled”数据集附加到 radioButtonList 控件。我还需要监视对 radioButtonlist 控件的更改并保存到数据库。

所以,我有两个与控件相关的方法需要: 从数据库填充值:DataSet 中的 TextBox 和 RadioButtonList 将值保存到数据库:选中 TextBox 和 RadioButton,将保存到表中。

如果这不起作用,我的替代方法是从数据库中获取 XML,然后从这个 XML 结构构造一个 XSLt。如果需要,我可以添加通过 AJAX 调用数据库的事件,以保存更新的单选按钮/文本框。我需要额外的代码来确保 javascript 方法也能正常工作。只是想在采用 XML 方式之前测试数据集方法..

我的 aspx.cs 代码隐藏执行此操作。

protected void Page_Load(object sender, EventArgs e)
{
    Populate();
}

private void Populate()
{
    DataSet dataSet = CustomCode.GetDataSet(somethingPassedIn);
    DataRow row = dataSet .Tables["Table1"].Rows[0];
    txBox.Text = row["Name"].ToString();
    //How to populate RadioButtonList with Yes/No checked appropriately? 
    //A bit column of 1/0 is returned per what needs to be checked 
    //(1=> Yes should be selected, 0=>No should be selected) 
}

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SaveData(); 
}
private void SaveData()
{
    CustomCode.SaveData(txBox.Text.Trim(), radioButtonList?);       
}

【问题讨论】:

    标签: c# dataset radiobuttonlist


    【解决方案1】:

    绑定:

    if((bool)row["IsEnabled"])
    {
        radioButtonList.SelectedValue = "Yes";
    }
    else
    {
        radioButtonList.SelectedValue = "No";
    }
    

    保存:

    bool isEnabled;
    if(radioButtonList.SelectedValue = "Yes")
    {
        isEnabled = true;
    }
    CustomCode.SaveData(txBox.Text.Trim(), isEnabled);
    

    【讨论】:

    • 忘了问,这会只加载一个文本框,1个单选按钮吗?我有一组行被返回,需要使用 listview/gridView 做同样的事情 - 从数据库中的列填充一组文本框和单选按钮。谢谢!
    • 设置你的gridview并使用RowDataBound事件在每一行中找到文本框和radiobuttin,并像我上面提到的那样绑定数据。你可以回顾这个问题:stackoverflow.com/questions/645415/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 2012-05-26
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多