填充两个列表框 [英] Populate Two List Boxes
本文介绍了填充两个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写了一些代码来填充列表框.更改列表框索引后,表单将回发.然后,第二个列表框调用与第一个列表框相同的代码.但是由于某种原因,它没有填充第二个列表框.请帮忙
Hi, I have written some code to populate a listbox. When the listbox index is changed the form is posted back. Then the second listbox calls the same code as the first listbox. But for some reason it is not populating the second listbox. Please help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
private OleDbConnection m_Con = new OleDbConnection();
private OleDbDataAdapter m_DA;
private DataTable m_DT = new DataTable();
private OleDbCommandBuilder m_CB;
private string m_SQL;
private String m_SQLLanguages = "SELECT * FROM CK_Language";
private String m_SQLCategory = "SELECT * FROM CK_Category";
private String m_SQLItems = "SELECT * FROM CK_Language";
private string m_DbName = "CodeKeep.accdb";
private string m_ColumnName;
private int m_TableCounter;
private int m_Language;
private string m_Category;
private bool m_CheckLanguage = false;
private bool m_CheckCategory = false;
private DropDownList m_DropDownList;
protected void OpenConnection(string m_DbName, string m_SQL)
{
m_Con.ConnectionString = "Provider=Microsoft.Ace.OleDb.12.0; Data Source=|DataDirectory|CodeKeep.accdb";
m_Con.Open();
m_DA = new OleDbDataAdapter(m_SQL, m_Con);
m_CB = new OleDbCommandBuilder(m_DA);
m_DA.Fill(m_DT);
m_Con.Close();
}
protected void PopulateListbox(string m_ColumnName, DropDownList m_DropDownList)
{
m_DropDownList.DataSource = m_DT;
m_DropDownList.DataTextField = m_DT.Columns[m_ColumnName].ColumnName.ToString();
m_DropDownList.DataBind();
m_DropDownList.Items.Insert(0, "Select Language");
}
protected void Page_Load(object sender, EventArgs e)
{
m_CheckLanguage = Convert.ToBoolean(ViewState["CheckLanguage"]);
m_CheckCategory = Convert.ToBoolean(ViewState["CheckCategory"]);
if (m_CheckLanguage == false)
{
//Sets variable equal to required sql select statement.
m_SQL = m_SQLLanguages;
//Calls void, passing in database name and sql statement.
OpenConnection(m_DbName, m_SQL);
//Sets variable equal to database column name.
m_ColumnName = "CK_Language";
//
m_DropDownList = dropDownListLanguage;
//Calls void, passing in variable.
PopulateListbox(m_ColumnName, m_DropDownList);
m_CheckLanguage = true;
ViewState["CheckLanguage"] = m_CheckLanguage;
m_CheckCategory = false;
ViewState["CheckCategory"] = m_CheckCategory;
m_CheckCategory = true;
}
if (m_CheckCategory == false)
{
m_DT.Rows.Clear();
m_Con.Dispose();
m_DA.Dispose();
m_DT.Dispose();
//Sets variable equal to required sql select statement.
m_SQL = m_SQLCategory;
//Calls void, passing in database name and sql statement.
OpenConnection(m_DbName, m_SQL);
//Sets variable equal to database column name.
m_ColumnName = "CK_Category";
//
m_DropDownList = dropDownListCategory;
//Calls void, passing in variable.
PopulateListbox(m_ColumnName, m_DropDownList);
m_CheckLanguage = true;
ViewState["CheckLanguage"] = m_CheckLanguage;
//m_CheckCategory = true;
//ViewState["CheckCategory"] = m_CheckCategory;
}
}
protected void buttonSave_Click(object sender, EventArgs e)
{
}
}
推荐答案
在我离开之前...我想我发现了问题.您处置连接,然后再也不创建新的连接.您不需要处理它...但是,如果要这样做,请在之后立即将其设置为新的连接.
我正在尝试遵循逻辑...如果我离开了,请告诉我
您的页面上只有一个DropDownListBox
.第一次,我不能告诉您是否应该首先检查类别或语言.我假设应该先检查语言.
如果是这样,它将使用语言"选项填充列表框.然后将CheckLanguage
设置为true,将CheckCategory
设置为false.这两个存储在ViewState
中,应在回发时进行维护.
然后,必须将m_CheckCategory
设置为true
,以确保它不会加载列表框中的类别"选项.至于良好的编码,为什么不使用return;
代替m_CheckCategory = true;
?您无需在该代码部分中进行任何其他操作,只需退出子例程即可.
因此,下次,它会检查ViewState
和CheckLanguage
应该是true
并且CheckCategory
应该是false
,这会将类别加载到列表框中.
因此,对于第一个答案,将CheckCategory
设置为true
的原因.
但是,对于最初的问题...您是否已第二次执行步骤以查看出了什么问题?m_DT.Columns[m_ColumnName]
中有什么吗?看起来您有两个表...一个CK_Language
和一个CK_Category
其中只有一列...与表名称相同.列名设置正确吗?
至于标准编码实践,如果要检查m_CheckLanguage
或m_CheckCategory
到false
,为什么还要将它们设置为false
?这与常识相反.如果m_CheckLanguage
设置为true
,则将假定您应该检查语言.您是否意识到您将m_DbName
传递给您的OpenConnection
,然后再也不使用它了?短语OpenConnection
表示您只是在打开连接.尝试使用PopulateDataTable
代替.您还要创建一个CommandBuilder
,然后再将其用于任何用途.我也不确定dropDownListCategory
和dropDownListLanguage
的来源.
下次,遍历每个步骤,看看它是否按照预期的方式工作.您应该第二次访问m_Con.ConnectionString
并看到它已被丢弃.
Before I go off...I think I found the problem. You Dispose of the connection and then never create a new one. You don''t need to dispose of it...but if you''re going to, set it to a new connection right after.
I''m trying to follow the logic...tell me if I''m off
You have a singleDropDownListBox
on your page. The first time through, I can''t tell if Category or Language is supposed to be checked first. I''ll assume that Language is supposed to be checked first.
If so, it populates the listbox with Language options. It then setsCheckLanguage
equal to true andCheckCategory
equal to false. Those two are stored inViewState
which should be maintained on postbacks.
Then, you have to setm_CheckCategory
totrue
to make sure it doesn''t load the Category options in the listbox. As to good coding, why not instead ofm_CheckCategory = true;
just usereturn;
? You don''t need to do anything else in that section of code, you can simply quit the subroutine.
So, the next time around, it checks theViewState
andCheckLanguage
should betrue
andCheckCategory
should befalse
, which should load the categories into the listbox.
So, to the first answer, there''s a reason whyCheckCategory
is set totrue
.
But, to the original question...have you stepped through the second time to see what''s going wrong? Is there anything inm_DT.Columns[m_ColumnName]
? It looks like you have two tables...oneCK_Language
and oneCK_Category
that have only one column in them...with the same name as the table. Are the column names set correctly?
And as to standard coding practice, why do you set them_CheckLanguage
orm_CheckCategory
tofalse
if you''re supposed to check them? That''s opposite of common sense. Ifm_CheckLanguage
is set totrue
, you would assume you were supposed to check the language. And do you realize that you pass inm_DbName
to yourOpenConnection
and then never use it? And the phraseOpenConnection
implies that you''re just opening the connection. Try,PopulateDataTable
instead. You also create aCommandBuilder
and then never use it for anything. I''m also not sure wheredropDownListCategory
anddropDownListLanguage
come from.
Next time, go through each step and see if it''s working the way it''s supposed to. You should have gotten tom_Con.ConnectionString
the second time and see that it was disposed of.
m_CheckCategory = true;
}
if (m_CheckCategory == false)
{
此代码可能是您的问题.
您将m_CheckCategory设置为true.
然后,您运行If
条件来检查是否是false
来填充其他列表框(显然永远不会运行).
您的逻辑可能需要更正.
This code could be your problem.
You set m_CheckCategory to true.
You then run a If
condition to check if it is false
to populate your other list box (which obviously will never run).
Your logic here might need to be corrected.
这篇关于填充两个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文