本文介绍了如何在具有多重选择的类文件中使用选定的列表框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai,
我有一个列表框控件有多个选择。它正在填充sql表中的值。在下面的代码中lstPrimaryImpactedArea是列表框名称如果我们选择多个项目,它应该插入到sql table.It工作在UI.But如果我们使用dataaccesslayer我们如何解决这个问题。
Hai,
I have a listbox control that having multiple selections.It is populating the values from sql table.In the below code "lstPrimaryImpactedArea" is listbox name.If we select multiple items it should be inserted into sql table.It is working in UI.But if we use dataaccesslayer how we can solve this.
<tr>
<td align="right">
<asp:Label ID="lblPrimaryImpactedArea" runat="server" Text="Primary Impact Area"></asp:Label>
</td>
<td>
<asp:ListBox ID="lstPrimaryImpactedArea" runat="server" SelectionMode="Multiple">
</asp:ListBox>
</td>
</tr>
foreach (ListItem item in lstPrimaryImpactedArea.Items)
{
if (item.Selected)
{
try
{
lblMessage.Text = lblMessage.Text + item.Text+",";
cmd.Parameters[
"@primaryimpactedarea"].Value = lblMessage.Text;
// string s;
// string str = item.Text + "," ;
// cmd.Parameters[
// "@primaryimpactedarea"].Value = str;
}
catch (Exception ex)
{
}
}
}
提前致谢
Thanks in advance
SqlCommand dCmd = new SqlCommand("InsertBugsdata", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@bugnumber", bugs.BugNumber);
dCmd.Parameters.AddWithValue("@publishtopartner", bugs.PublishToPartner);
dCmd.Parameters.AddWithValue("@impactscale", bugs.ImpactScale);
dCmd.Parameters.AddWithValue("@primaryimpactedarea", bugs.PrimaryImpactedArea);
dCmd.Parameters.AddWithValue("@secondaryimpactedarea", bugs.SecondaryImpactedArea);
dCmd.Parameters.AddWithValue("@impactedentity", bugs.ImpactedEntity);
dCmd.Parameters.AddWithValue("@impacttype", bugs.ImpactType);
dCmd.Parameters.AddWithValue("@percentimpact", bugs.PercentImpact);
dCmd.Parameters.AddWithValue("@title", bugs.Title);
dCmd.Parameters.AddWithValue("@description", bugs.Description);
dCmd.Parameters.AddWithValue("@defectdiscovered", bugs.DefectDiscovered);
dCmd.Parameters.AddWithValue("@eta", bugs.ETA);
dCmd.Parameters.AddWithValue("@defectstatus", bugs.DefectStatus);
dCmd.Parameters.AddWithValue("@defectstatusdetails", bugs.DefectStatusDetails);
dCmd.Parameters.AddWithValue("@defectclosetime", bugs.DefectCloseTime);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
推荐答案
private void savedata()
{
BusinessLayer objBusiness = new BusinessLayer();
int[] arySelectedItems = lstPrimaryImpactedArea.GetSelectedIndices();
for (int i = 0; i < arySelectedItems.Length; i++)
{
string text = lstPrimaryImpactedArea.Items[arySelectedItems[i]].Text;
objBusiness.SaveData(text);
}
}
protected void btn_Click(object sender, EventArgs e)
{
savedata();
}
}
public class BusinessLayer
{
DataLayer objDataLayer;
public void SaveData(string data)
{
objDataLayer = new DataLayer();
objDataLayer.SaveData(data);
}
}
public class DataLayer
{
public void SaveData(string data)
{
// write ur ado code to save the data in the database
}
}
这篇关于如何在具有多重选择的类文件中使用选定的列表框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!