2017-02-21 8 views
1

Внизу datalist представляет собой набор вопросов и ответов.Как выбрать значение каждого переключателя в datalist?

Как вставить выбранное пользователем правильное значение переключателя ответа в базу данных, когда пользователь нажимает кнопку «Окончательный подарок»?

<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" onselectedindexchanged="rd_CS_CheckedChanged"> 
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" /> 
      <AlternatingItemStyle BackColor="#F7F7F7" /> 
      <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> 
      <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" /> 
      <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" /> 
      <ItemTemplate> 
       Q: 
      <asp:Label ID="QLabel" runat="server" Text='<%# Eval("Q") %>' /> 
      <br /> 
       A: 
       <asp:RadioButton ID="rd_CS" runat="server" GroupName="Casi" OnCheckedChanged="rd_CS_CheckedChanged" Text='<%# Eval("A") %>'></asp:RadioButton> 
       <br /> 
       B: 
       <asp:RadioButton ID="rd_CS2" runat="server" GroupName="Casi" OnCheckedChanged="rd_CS_CheckedChanged" Text='<%# Eval("B") %>'></asp:RadioButton> 
       <br /> 
       C: 
       <asp:RadioButton ID="rd_CS3" runat="server" GroupName="Casi" OnCheckedChanged="rd_CS_CheckedChanged" Text='<%# Eval("C") %>'></asp:RadioButton> 
       <br /> 
       D: 
       <asp:RadioButton ID="rd_CS4" runat="server" GroupName="Casi" OnCheckedChanged="rd_CS_CheckedChanged" Text='<%# Eval("D") %>'></asp:RadioButton> 

       <p style="color: #FF3300"> 
      <asp:Label ID="Correct_AnswerLabel" runat="server" 
       Text='<%# Eval("Correct_Answer") %>' Visible="False" /></p> 

    </ItemTemplate> 
    <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> 
</asp:DataList> 

    <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
     DataFile="~/App_Data/Quize.mdb" 
     SelectCommand="SELECT [Q],[A], [B], [C], [D], [Correct Answer] AS Correct_Answer FROM [QuizData]"> 
    </asp:AccessDataSource> 


    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 

ответ

1

В Button1_Click на вашем коде вы можете улучшить этот метод:

protected void Button1_Click(object sender, EventArgs e) 
    { 
     foreach (DataListItem item in DataList1.Items) 
     { 
      RadioButton rd_CS = (RadioButton)item.FindControl("rd_CS"); 
      RadioButton rd_CS2 = (RadioButton)item.FindControl("rd_CS2"); 
      RadioButton rd_CS3 = (RadioButton)item.FindControl("rd_CS3"); 
      RadioButton rd_CS4 = (RadioButton)item.FindControl("rd_CS4"); 

      if (rd_CS.Checked) 
      { 
       Insert(rd_CS.Text); //Here you can insert whatever value you want, I tried with Text of radiobutton 
      } 
      if (rd_CS2.Checked) 
      { 
       Insert(rd_CS2.Text); //Here you can insert whatever value you want, I tried with Text of radiobutton 
      } 
      if (rd_CS3.Checked) 
      { 
       Insert(rd_CS3.Text); //Here you can insert whatever value you want, I tried with Text of radiobutton 
      } 
      if (rd_CS4.Checked) 
      { 
       Insert(rd_CS4.Text); //Here you can insert whatever value you want, I tried with Text of radiobutton 
      } 
     } 
    } 

И определение функции вставки будет такой же, как:

private void Insert(string value) 
    { 
     //Your code here to save on database 
     OleDbConnection connection = new OleDbCommand("Your sql connection String"); 
     OleDbCommand command = new OleDbCommand("Your sql insert query"); 
     command.Connection = connection; 

     //Parámeters of command 
     OleDbParameter param = new OleDbParameter("Parameter name and next your type", OleDbType.VarChar); 
     param.Value = value; 

     command.Parameters.Add(param); 

     command.Connection.Open(); 

     command.ExecuteNonQuery(); 

     command.Connection.Close(); 

     //Your value is saved now 
    } 

Это, как вы можете сохранить все проверено radobutton on datalist вы задали

0

Это мой codebehind. Когда я нажимаю кнопку, то в моей базе данных сохраняются повторяющиеся выбранные значения переключателя. это моя проблема. Мне нужно только уникальное значение в моей базе данных

защищен недействительным rd_CS_CheckedChanged (объект отправителя, EventArgs е) { строка myRadioText = String.Empty;

foreach (DataListItem item in DataList1.Items) 
    { 

     RadioButton rd_CS = (RadioButton)item.FindControl("rd_CS"); 
     RadioButton rd_CS2 = (RadioButton)item.FindControl("rd_CS2"); 
     RadioButton rd_CS3 = (RadioButton)item.FindControl("rd_CS3"); 
     RadioButton rd_CS4 = (RadioButton)item.FindControl("rd_CS4"); 


     if (rd_CS != null && rd_CS.Checked) 
     { 
      myRadioText = rd_CS.Text; 
      Label1.Text = myRadioText.ToString(); 

     } 
     else if (rd_CS2 != null && rd_CS2.Checked) 
     { 
      myRadioText = rd_CS2.Text; 
      Label1.Text = myRadioText.ToString(); 
     } 
     else if (rd_CS3 != null && rd_CS3.Checked) 
     { 
      myRadioText = rd_CS3.Text; 
      Label1.Text = myRadioText.ToString(); 
     } 
     else if (rd_CS4 != null && rd_CS4.Checked) 
     { 
      myRadioText = rd_CS4.Text; 
      Label1.Text = myRadioText.ToString(); 
     } 
     string str = Server.MapPath("~/App_Data/Quize.mdb"); 
     OleDbConnection ole = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str + ";Persist Security Info=True"); 
     ole.Open(); 
     OleDbCommand cmd = new OleDbCommand("insert into Userdata values ('" + Label1.Text.Trim().Replace("'", "''") + "','" + Label1.Text.Trim().Replace("'", "''") + "',)", ole); 
     cmd.ExecuteNonQuery(); 

}

Смежные вопросы