我有3个复选框,id是chkIPP,chkOutReach和chkCCA。当用户选中其中一个复选框时,该值将插入BlogType列字段中。我想要插入的3个值是IPP故事,OutReach活动和CCA。我只想要只检查一个复选框。只有一个值将插入到blogType列字段中。
我认为近似控件应该是单选按钮或下拉列表,因为只能选择一个并插入数据库,但我的老师告诉我使用复选框。因此,用户将键入3文本框,并选中其中一个复选框,并将值插入数据库。我已经编写了3个文本框。留下复选框。感谢
关注@serhads建议。我做错了吗?
protected void btnSubmit_Click(object sender, EventArgs e)
{
string AdminNumber = Convert.ToString(txtAdmin.Text);
string Name = Convert.ToString(txtName.Text);
string BlogStory = Convert.ToString(txtStory.Text);
insertGameRecord(AdminNumber, Name, BlogStory);
}
private void insertGameRecord(string admin, string name, string story)
{
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "INSERT EntryTable(AdminNumber, Name, BlogStory, DateEntry) Values(@AdminNumber, @Name, @BlogStory, @DateEntry)";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@AdminNumber", admin);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@BlogStory", story);
cmd.Parameters.Add("DateEntry", SqlDbType.DateTime);
cmd.Parameters["DateEntry"].Value = DateTime.Now;
myConnect.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{ lblError.Text = "Record Updated";
bindResultGridView();
}
else { lblError.Text = "Update fail"; }
myConnect.Close();
}
catch(Exception)
{
lblError.Text = "Please enter correct data";
}
}
答案 0 :(得分:1)
您是否有另一个BlogType表?如果是的话,我建议你在BlogType表和EntryTable之间创建另一个表。您可以在此表中存储选定的复选框值。表可以是这样的。
ID << primary key
BlogID << EntryTable referance id
BlogTypeID << BlogType table referance id
通过这种方式,您可以为BlogId为每个选定的复选框值插入新记录。
答案 1 :(得分:1)
有两种方法可以做到:
<强> 1。使用JQuery:
在每个复选框的点击事件中,使用JQuery禁用所有三个复选框,如下面的代码示例中所述:
HTML:
<asp:CheckBox runat="server" ID="chkIPP" CssClass="check" />
<asp:CheckBox runat="server" ID="chkOutReach" CssClass="check" />
<asp:CheckBox runat="server" ID="chkCCA" CssClass="check" />
<asp:Button runat="server" ID="buttonSubmit" Text="Submit" OnClick="buttonTest_OnClick" />
JavaScript:
$('.check').click(function () {
$('.check').each(function () {
$(this).prop('disabled', 'disabled');
});
});
<强> 2。将CheckeboxList与OnSelectedIndexChanged事件一起使用:
HTML:
<asp:CheckBoxList runat="server" ID="cblType" OnSelectedIndexChanged="cblType_OnSelectedIndexChanged" AutoPostBack="True">
<Items>
<asp:ListItem Text="IPP Stories" Value="IPP Stories"></asp:ListItem>
<asp:ListItem Text="OutReach Activities " Value="OutReach Activities"></asp:ListItem>
<asp:ListItem Text="CCA" Value="CCA"></asp:ListItem>
</Items>
</asp:CheckBoxList>
<asp:Button runat="server" ID="buttonSubmit" Text="Submit" OnClick="buttonTest_OnClick" />
代码背后:
protected void cblType_OnSelectedIndexChanged(object sender, EventArgs e)
{
this.cblType.Enabled = false;
}
按钮提交点击活动:
protected void btnSubmit_Click(object sender, EventArgs e)
{
string AdminNumber = Convert.ToString(txtAdmin.Text);
string Name = Convert.ToString(txtName.Text);
string BlogStory = Convert.ToString(txtStory.Text);
string BlogType = cblType.SelectedValue;
insertGameRecord(AdminNumber, Name, BlogStory, BlogType);
}
答案 2 :(得分:0)
我假设您创建了名为"drpBlogType"
protected void btnSubmit_Click(object sender, EventArgs e)
{
string AdminNumber = Convert.ToString(txtAdmin.Text);
string Name = Convert.ToString(txtName.Text);
string BlogStory = Convert.ToString(txtStory.Text);
// get value or dropdown
string BlogType = drpBlogType.SelectedValue;
insertGameRecord(AdminNumber, Name, BlogStory,BlogType);
}
private void insertGameRecord(string admin, string name, string story,string BlogType)
{
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "INSERT EntryTable(AdminNumber, Name, BlogStory, DateEntry,BlogType) Values(@AdminNumber, @Name, @BlogStory, @DateEntry,@BlogType)";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@AdminNumber", admin);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@BlogStory", story);
cmd.Parameters.AddWithValue("@BlogType", BlogType);
cmd.Parameters.Add("DateEntry", SqlDbType.DateTime);
cmd.Parameters["DateEntry"].Value = DateTime.Now;
myConnect.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{ lblError.Text = "Record Updated";
bindResultGridView();
}
else { lblError.Text = "Update fail"; }
myConnect.Close();
}
catch(Exception)
{
lblError.Text = "Please enter correct data";
}
}