0

我正在尝试在下拉列表中绑定我的打印机名称,并在我的下拉列表中管理显示所有打印机名称。当我尝试获取所选项目的值时,它会显示所有打印机的第一个值。下面是代码

绑定打印机名称的代码:

PrintModuleAX printModuleAX = new PrintModuleAX();
var result = printModuleAX.GetAllPrinterNames();
JObject o = JObject.Parse(result);
JArray sizes = (JArray)o["PrinterNames"];
var dt = new DataTable();
dt.Columns.Add("PrinterValue");
dt.Columns.Add("PrinterName");
for (int i = 0; i < sizes.Count; i++)
{
    dt.Rows.Add((string)sizes[i], (string)sizes[i]);
}
ddlPrinterName.DataSource = dt;
ddlPrinterName.DataTextField = dt.Columns["PrinterName"].ToString();
ddlPrinterName.DataValueField = dt.Columns["PrinterValue"].ToString();
ddlPrinterName.DataBind();

获取所选值的代码:

var printername = ddlPrinterName.Text.ToString();
var printername1 = ddlPrinterName.SelectedValue.ToString();
var printername2 = ddlPrinterName.SelectedItem.ToString();

printername, printername1,printername2即使我选择了不同的打印机,也会显示相同的值。

4

2 回答 2

2

检查数据绑定是否在每次页面加载时都发生。(使用 if (!page.ispostback))

于 2012-12-28T08:57:58.657 回答
1

试试下面的代码

ddlPrinterName.DataSource = dt;
//just specify the column name
ddlPrinterName.DataTextField = "PrinterName"; 
ddlPrinterName.DataValueField = "PrinterValue";
ddlPrinterName.DataBind();



var printername1 = ddlPrinterName.SelectedItem.Text; //gives text
var printername2 = ddlPrinterName.SelectedItem.Value; //gives value
于 2012-12-28T09:03:50.957 回答