处理动态创建的复选框的事件,并在每个新选择的附加位置附加数据 [英] Handling events of dynamically created checkboxes and append data below place holder with every newe selection
问题描述
我为主目的地创建了动态复选框,如加尔各答,德里等(来自数据库),当我检查说像加尔各答它在新的动态创建的复选框中显示加尔各答内的子地点,再次,如果我检查另一个主要目的地说德里kolkata的子目的地应该保留在那里,从德里添加更多目的地,同样应该继续根据我的主要目的地检查添加子目的地。如何实现
为主要目的地添加动态复选框的代码
I have created dynamic check box for Main destination like Kolkata ,Delhi etc (from database), when i check say like Kolkata it shows sub places inside Kolkata in new dynamically created checkboxes,again if i checkon another main destination say Delhi the sub destination of kolkata should remain there with more destinations added from Delhi similarly it should keep on adding sub destination as per my main destination check. How to make it happen
Code for synamic adding dynamic check box for main destination
private void AddingDynamicCheckBoxList()
{
SqlConnection conn = new SqlConnection(cls.GetConnectionString());
SqlCommand cmd = new SqlCommand("SELECT Destination_Name FROM Destination_Master", conn);
DataTable dt = new DataTable();
SqlDataAdapter sqlAdp = new SqlDataAdapter(cmd);
sqlAdp.Fill(dt);
int countDestination = dt.Rows.Count;
for (int loop = 0; loop < countDestination; loop++)
{
//checked changed event
CheckBox chk;
chk = new CheckBox();
chk.ID = loop.ToString();
chk.Text = dt.Rows[loop][0].ToString();
chk.AutoPostBack = true;
PlaceHolder1.Controls.Add(chk);
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
PlaceHolder1.Controls.Add(new LiteralControl(" "));
//button click event
//CheckBoxList ChkboxList = new CheckBoxList();
//ChkboxList.ID = "Chkbox" + loop.ToString();
//ChkboxList.Items.Add(new ListItem(dt.Rows[loop][0].ToString()));
//PlaceHolder1.Controls.Add(ChkboxList);
}
}
支票更改和填写子目的地的代码
Code for check change and fill sub destination
private void chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox cc = (CheckBox)(sender);
cc.AutoPostBack = true;
if (cc.Checked == true)
{
// Response.Write(cc.Text);
fill_sub_destination(cc.Text);
}
else
{
}
}
private void fill_sub_destination(string desName)
{
DataTable dtSubDestination = new DataTable();
DataTable dtTemp = new DataTable();
string des_id = cls.FetchSingleval("SELECT Destination_ID FROM Destination_Master WHERE Destination_Name='" + desName + "'");
SqlConnection conn1 = new SqlConnection(cls.GetConnectionString());
SqlCommand cmd = new SqlCommand("SELECT Sub_Destination_Name FROM Sub_Destination WHERE Destination_ID='" + des_id + "'", conn1);
SqlDataAdapter sqlAdp = new SqlDataAdapter(cmd);
if (dtSubDestination.Rows.Count == 0)
{
sqlAdp.Fill(dtSubDestination);
}
else
{
sqlAdp.Fill(dtTemp);
}
dtSubDestination.Merge(dtTemp);
dtSubDestination.AcceptChanges();
int countDestination1 = dtSubDestination.Rows.Count;
for (int loop = 0; loop < countDestination1; loop++)
{
CheckBox chkSub;
chkSub = new CheckBox();
chkSub.ID = "chkSub" + loop.ToString();
chkSub.Text = dtSubDestination.Rows[loop][0].ToString();
PlaceHolder holder = new PlaceHolder();
holder.Controls.Add(chkSub);
PlaceHolder2.Controls.Add(holder);
PlaceHolder2.Controls.Add(new LiteralControl(" "));
}
}
推荐答案
将您的第一个循环更改为
for(int loop = 0; loop< countDestination; loop ++)
{
// checked改变了事件
CheckBox chk;
chk = new CheckBox();
chk.ID = loop.ToString();
chk.Text = dt.Rows [loop] [0] .ToString();
chk.Attributes.Add(onclick,javascript:FillSub(this,'' + chk.Text +''););
PlaceHolder1.Controls.Add(chk);
PlaceHolder1.Controls.Add(new LiteralControl());
}
在按钮上添加javascript函数和一个隐藏字段。
Changed your first loop to
for (int loop = 0; loop < countDestination; loop++)
{
//checked changed event
CheckBox chk;
chk = new CheckBox();
chk.ID = loop.ToString();
chk.Text = dt.Rows[loop][0].ToString();
chk.Attributes.Add("onclick", "javascript:FillSub(this,''" + chk.Text + "'');");
PlaceHolder1.Controls.Add(chk);
PlaceHolder1.Controls.Add(new LiteralControl(" "));
}
Add javascript function and One hidden field and on Button.
function FillSub(obj,textName)
{
//Add one hiddenfield as hidndesName
//And one Button as uxBtnRefresh add style to it as Display:none and add OnClick event of the Button.
if(obj.checked)
{
document.getElementById('hidndesName').value=textName;
document.getElementById('uxBtnRefresh').click();
} else
{
document.getElementById('hidndesName').value='';
}
}
在.cs上添加按钮的OnClick事件
Add OnClick Event of the button on .cs
protected void uxBtnRefresh_Click(object sender, EventArgs e)
{
//call your function directly fill_sub_destination(hidndesName.value)
}
这篇关于处理动态创建的复选框的事件,并在每个新选择的附加位置附加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!