anu

anu

  • 2.1k
  • 15
  • 2.7k

how to store repeater control value in database

Mar 28 2018 1:03 AM
design: 
 
<!DOCTYPE html>
<div>
<br />
<asp:DropDownList ID="ddlsubject1" runat="server" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlsubject1_SelectedIndexChanged">
<asp:ListItem>--SELECT SUBJECT CODE--</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
<br />
<br />
<br />
<br />
<asp:Repeater ID="rptQues" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand="rptQues_ItemCommand">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("question_id") %>' Visible="False" />
<asp:Label ID="lblques" runat="server" Text='<%# Eval("question_name") %>' />
<asp:RadioButtonList ID="rdoTest" runat="server"></asp:RadioButtonList>
<%-- <asp:PlaceHolder ID="plchldr" runat="server">
</asp:PlaceHolder>--%>
<%--<asp:RadioButtonList runat="server" ID="rdolst"></asp:RadioButtonList>--%>
<asp:Repeater ID="rptOption" runat="server">
<ItemTemplate >
<%-- <asp:RadioButton ID="rbopt" runat="server" Text='<%# Eval("Options") %>' />--%>
<%-- <asp:RadioButton GroupName="abc" runat="server" Text='<%# Eval("Options") %>' />--%>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" DataValueField='<%# Eval("option_name") %>'>
<%-- <asp:ListItem Text='<%# Eval("Options") %>'></asp:ListItem>--%>
</asp:RadioButtonList>
</ItemTemplate>
</asp:Repeater>
<br />
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="Button1" runat="server" Text="NEXT" OnClick="Button1_Click" />
<br />
</div>
</asp:Content>
 
 
code:
 
public partial class repeateraspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
viewsubjectdropdown();
}
getData(this.User.Identity.Name);
}
public void viewsubjectdropdown()
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=Feedback;User ID=sa;Password=admin28");
con.Open();
string com = "Select subject_code,subjectid from tbl_subject";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
ddlsubject1.DataSource = dt;
ddlsubject1.DataTextField = "subject_code";
ddlsubject1.DataValueField = "subjectid";
ddlsubject1.DataBind();
con.Close();
}
private void getData(string user)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=Feedback;User ID=sa;Password=admin28");
con.Open();
SqlCommand cmd = new SqlCommand("select question_id,question_name from tbl_question", con);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
rptQues.DataSource = dt;
rptQues.DataBind();
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=Feedback;User ID=sa;Password=admin28");
con.Open();
//HiddenField hdn = new HiddenField();hdn= (HiddenField)FindControl("HiddenField1");
// string carrierName = ((String)e.Item.DataItem)["carrierName"].ToString();
DataRowView dr = (DataRowView)e.Item.DataItem;
string strcarrierName = Convert.ToString(dr["question_id"]);
SqlCommand cmd = new SqlCommand("select tbl_option.option_name from tbl_option inner join tbl_question_option_mapping on tbl_question_option_mapping.option_id=tbl_option.option_id inner join tbl_question on tbl_question.question_id=tbl_question_option_mapping.question_id where tbl_question.question_id='" + strcarrierName + "'", con);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Repeater rptOrders = e.Item.FindControl("rptOption") as Repeater;
rptOrders.DataSource = dt;
rptOrders.DataBind();
//RadioButtonList rdoLst = new RadioButtonList();
//rdoLst.DataSource = dt;
//rdoLst.DataTextField = "Options";
//rdoLst.DataBind();
RadioButtonList rdot = e.Item.FindControl("rdoTest") as RadioButtonList;
rdot.DataSource = dt;
rdot.DataTextField = "option_name";
rdot.DataBind();
con.Close();
//PlaceHolder plc = e.Item.FindControl("plchldr") as PlaceHolder;
//plc.Controls.Add(rdoLst);
}
}
}
protected void rptQues_ItemCommand(object source, RepeaterCommandEventArgs e)
{
}
protected void ddlsubject1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=Feedback;User ID=sa;Password=admin28");
con.Open();
string com = "select tbl_staff.staff_name from tbl_staff join tbl_subject_staff_mapping on tbl_staff.staff_id=tbl_subject_staff_mapping.staff_id where tbl_subject_staff_mapping.subjectid='" + ddlsubject1.SelectedValue + "'";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
if (dt.Rows.Count > 0)
{
Label2.Text = dt.Rows[0]["staff_name"].ToString();
}
// DropDownList1.DataSource = dt;
// DropDownList1.DataTextField = "staff_name";
// //DropDownList1.DataValueField = "substaffmap_id";
// DropDownList1.DataBind();
//// Label2.Text = Convert.ToString(dt.Rows[0][0]);
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=Feedback;User ID=sa;Password=admin28");
con.Open();
string selectedItem = ddlsubject1.SelectedItem.Text;
for (int i = ddlsubject1.Items.Count - 1; i >= 0; i--)
{
if (ddlsubject1.Items[i].Text == selectedItem) ddlsubject1.Items.RemoveAt(i);
}
if (ddlsubject1.Items.Count == 1)
{
Response.Redirect("Webform3.aspx");
}
//viewsubjectdropdown();
Label2.Text = "";
con.Close();
}
}
}
 
 

Answers (1)