简体   繁体   中英

Radio button selected value in Repeater control insert to the SQL database

I had implement the code that in the repeater control with radio button selection.

Now I want to insert the selected radio button value into a database.

Code:

<div id="ManageAddressPage" runat="server" style="margin-top: 10px">
    <asp:Repeater ID="ManageAddressRepeater" runat="server">
        <ItemTemplate>
            <div class="col-8 col-m-12" style="border: solid 1px LightGray">
                <div>
                    <asp:RadioButton ID="RadioButton1" runat="server" Text='<%#Eval("Name") %>' OnClick="javascript:singleselection(this.id)" />  
                </div>
            </div>
        </ItemTemplate>
    </asp:Repeater>

    <asp:Button ID="Button3" runat="server" CssClass="btnProfileSave" Text="Save Address" OnClick="Button3_Click" />
</div>

Javascript: to select one value

<script type="text/javascript">
function singleselection(rbid) {
    var rbutton = document.getElementById(rbid);
    var rbuttonlist = document.getElementsByTagName("input")

    for (i = 0; i < rbuttonlist.length; i++) {
        if (rbuttonlist[i].type == "radio" && rbuttonlist[i].id !== rbutton.id) {
            rbuttonlist[i].checked = false;
        }
    }
}
</script>

Well, ok, we should have more info here.

We assume some compnay, person or whatever (main table).

Then we assume that we display the child records of the address(s) and you want to select which one is active. We might have 2 or 5 address to select from.

So, this VERY much suggests that in the database WHICH Address is to be active is required here.

So, here is our table of address(s) - child table.

在此处输入图像描述

Ok, so we need to display perhaps some information from the main reocrd.

And then for the child records - the address, we need to list them out.

So, our markup looks like this:

  <div>
        <br />
        <asp:HiddenField ID="HotelID" runat="server" />

        <div>
            Hotel Name: <asp:label ID="txtHotel" runat="server" Font-Size="X-Large"></asp:label>
            <br />
        </div>
        <h3>Select Active Address</h3>

         <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div style="border-style:solid;color:black;width:250px;float:left">
        <asp:RadioButton ID="RadioButton1" runat="server"
            Checked = '<%# Eval("Active") %>'  
            Text = '<%# Eval("AddressLocation") %>'
            AutoPostBack="true"
            PKID = '<%# Eval("ID") %>'
            OnCheckedChanged="RadioButton1_CheckedChanged"
            />
            

        <div style="padding:5px;text-align:right">
            Location : <asp:TextBox ID="txtLoc" runat="server" Text ='<%# Eval("AddressLocation") %>' Width="130px" />
            <br />
            Address: <asp:TextBox ID="txtAddress" runat="server" Text ='<%# Eval("Address") %>'  Width="130px" />
            <br />
            City: <asp:TextBox ID="txtCity" runat="server" Text ='<%# Eval("City") %>'  Width="130px" />
            <br />
            <br />
        </div>
      </div>
       <div style="clear:both;height:10px"></div>
    </ItemTemplate>
</asp:Repeater>
        <br />
    </div>

And our display is thus this:

在此处输入图像描述

So, if we check any radio button, we have to set that row to Active.

The code looks like this:

We assume master reocrd = 19 - one hotel, person - whatever how you selected that.

protected void Page_Load(object sender, System.EventArgs e)
{
    if (IsPostBack == false)
    {
        HotelID.Value = 19;
        LoadData();
    }
}

public void LoadData()
{
    using (SqlCommand cmdSQL = new SqlCommand("SELECT * FROM tblAddress WHERE Hotel_ID = @ID",
                              new SqlConnection(My.Settings.TEST3)))
    {
        cmdSQL.Connection.Open();
        cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = HotelID.Value;
        Repeater1.DataSource = cmdSQL.ExecuteReader;
        Repeater1.DataBind();
        cmdSQL.Connection.Close();

        // show hotel name (parent reocord) - Parmater already set
        cmdSQL.Connection.Open();
        cmdSQL.CommandText = "SELECT HotelName from tblHotels WHERE ID = @ID";
        txtHotel.Text = cmdSQL.ExecuteScalar;
    }
}

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    RadioButton btnRadio = sender;

    // un-check all address - radio button can ONLY click if not selected, so we 
    // always assume checked = true

    using (SqlCommand cmdSQL = new SqlCommand("UPDATE tblAddress SET Active = 0 WHERE Hotel_ID = @ID", 
                              new SqlConnection(My.Settings.TEST3)))
    {
        cmdSQL.Connection.Open();

        cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = HotelID.Value;
        cmdSQL.ExecuteNonQuery();

        // Now set this address just clicked as active
        cmdSQL.CommandText = "UPDATE tblAddress SET Active = 1 WHERE ID = @ID";
        cmdSQL.Parameters.Clear();
        cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = btnRadio.Attributes.Item("PKID");
        cmdSQL.ExecuteNonQuery();

        LoadData();     // update display
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM