Shaik
Shaik

Reputation: 930

Save Dynamic radio button clicked data to database

I have a generated array of radio buttons based on user selects the data say if my database table has 10 rows then 10 groups of radio buttons are created.

My problem is I need to save the clicked radio button values along with the text box data which is next to the radio button on button click.

The below is the server side query to fetch the data from db and create the rows along with the radio button group and name of each in one text box.

What needs to be done is on click on button I need a query to save only radio button clicked val and all the text box val to be saved in db table as they are dynamically generated i have zero clue how to save then small help would be more help full for me.

int numberofrow1 = 0;
int numberofrow2 = 0;

DDccccc = DDccccc.SelectedValue;// store it in some variable;
string htmlStr = "";
using (MySqlConnection myConnection = new MySqlConnection(constr))
{
    string oString = "Select * from xxxxxxxxx WHERE xxxxxx=@cccc  order by xxxxxxxxxxx ASC";
    MySqlCommand oCmd = new MySqlCommand(oString, myConnection);
    oCmd.Parameters.AddWithValue("@cccc", DDccccc);

    myConnection.Open();
    using (MySqlDataReader oReader = oCmd.ExecuteReader())
    {
        if (oReader == null || !oReader.HasRows)
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('xxxxxxx')", true);
        }
        else
        {
            while (oReader.Read())
            {
                string Name = oReader["xxxxxxxxxx"].ToString();
                htmlStr += "<tr><td><input type='text' value='" + Name + "'/></td><td><input type='radio' name='Present" + numberofrow++ + "' value='Present'></td><td><input type='radio' name='Present" + numberofrow1++ + "' value='Absent'></td><td><input type='radio' name='Present" + numberofrow2++ + "' value='Leave'></td></tr>";
            }
        }
        myConnection.Close();
    }
}
LIST.InnerHtml = htmlStr;

Upvotes: 1

Views: 783

Answers (2)

Imad
Imad

Reputation: 7490

try

var val = Request.Form["Present1"]

Edit

If you don't have server id for any control then still you can access there values (as far as they are input). You can access there values using Form collection. To get all checkbox values in an array, I presume you have saved number of records in oReader somewhere in int totalRecords. So you can use following code to access those values as below.

List<string> chkValues = new List<string>();
for( int i = 1; i <= totalRecords; i++)
{
    chkValues.Add(Request.Form["Present"+i]);
}

Upvotes: 1

Vaibhav Chaurasiya
Vaibhav Chaurasiya

Reputation: 166

Hi You can try this : add radio button along with their id and runat='server' so we can easily able to find controls on .cs file :

<input type='radio' name='Present1' value='Present' runat="server" id="Present1">

and to find control on any event such like button click event :

   `protected void btnSubmit_Click(object sender, EventArgs e)
    {
        var contorl = Page.FindControl("Present1");
    }` 

Upvotes: 0

Related Questions