AJAX做的DropDownList联动,无法取到联动那个DropDownList的选中值。请大家帮帮忙。
贴代码,麻烦大家帮忙看看,谢谢。
.aspx
<script language="javascript" type="text/javascript" defer="defer">
function ShowName(id)
{
var res=top3.GetExpName(parseInt(id)).value;
var ddl=document.getElementById(" <%=DropDownList2.UniqueID %>");
ddl.length=0;
if(res)
{
for(var i=0;i <res.length;i++)
{
ddl.options.add(new Option(res[i]));
}
}
}
function GetName()
{
document.getelementbyid("ExpName").value=document.all.DropDownList2.options[document.all.DropDownList2.selectedIndex].text;
}
</script>
....
<TD class=title-text-h2 height=22>科室
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="SecName" DataValueField="SecID">
</asp:DropDownList> </TD>
<TD class=title-text-h2 height=22>姓名
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value=0 Selected="True">请选择医生 </asp:ListItem>
</asp:DropDownList> <asp:TextBox ID="ExpName" runat="server" Visible="false"> </asp:TextBox> </TD>
<td> </td>
<TD> <asp:ImageButton ID="submit" ImageUrl="~/pic/zjsearch/shosuo_niutu.gif" runat="server" OnClick="Button_Click" /> </TD>
</TR> </TBODY> </TABLE> </TD>
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(top3));//注册
if (!Page.IsPostBack)
{
DataTable dt = user.ShowSec();
this.DropDownList1.DataSource = user.ShowSec();
this.DropDownList1.DataTextField = "SecName";
this.DropDownList1.DataValueField = "SecID";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0, "请选择科室");
this.DropDownList1.Items[0].Selected = true;
this.DropDownList1.Attributes["onchange"] = "ShowName(this.options[selectedIndex].value)";
this.DropDownList2.Attributes.Add("onchange", "GetName()");
}
}
[AjaxPro.AjaxMethod]
public string[] GetExpName(int stateId)
{
DataTable dt = user.ShowExp(stateId).Table;
string[] name = new String[dt.Rows.Count + 1];
name[0] = "请选择医生";
for (int i = 1; i <= dt.Rows.Count; i++)
{
name[i] = dt.Rows[i - 1]["ExpName"].ToString();
}
return name;
}
protected void Button_Click(object sender, EventArgs e)
{
if (this.DropDownList1.SelectedItem.ToString() == "请选择科室" && this.DropDownList2.SelectedItem.ToString() == "请选择医生")
{
Response.Write(" <script language=javascript>alert('请选择要查询的条件!');window.close(); </script>");
Response.Write(" <script language=javascript>window.location.href='Default.aspx'; </script>");
}
else
{
DataTable dt = user.ShowExp(this.DropDownList1.SelectedItem.ToString(), this.ExpName.Text.Trim());
int id = int.Parse(dt.Rows[0]["ID"].ToString());
Response.Redirect("~/zjjs/Showexperts.aspx?ShowID=" + id);
}
}
this.ExpName.Text.Trim(),就是取不到值。
int id = int.Parse(dt.Rows[0]["ID"].ToString());
报错是这里,在0处没有任何行。但是我把this.ExpName.Text.Trim()写成”张三“,就可以正常显示。
请大家帮忙看看啊。