[英]ASP.NET codebehind can't get selected drop down list value (after set Javascript attribute)
任何人都知道它是如何導致 ASP.NET 代碼隱藏無法獲取所選值的。
我發現問題是 javascript function;
設置屬性部分:
process.options[process.selectedIndex].setAttribute("style", "color:red;font-weight:600;");
process.options[process.selectedIndex].setAttribute("disabled", "disabled");
如果我刪除這些 setAttribute 部分,ASP.NET 代碼隱藏可以獲取所選值。
有什么解決辦法嗎? 如果我堅持要使用 setAttribute 部分
樣本:
HTML
<html>
<table style="margin-left: auto; text-align: center; margin-right: auto; font-size: large; width: 50%" border="1">
<tr>
<td class="auto-style1">Process Name</td>
<td class="auto-style2">:</td>
<td class="auto-style3">
<asp:DropDownList ID="ddl_processname" runat="server" CssClass="ddl-style" onchange="focustester()">
<asp:ListItem Value="P1">Process 1</asp:ListItem>
<asp:ListItem Value="P2">Process 2</asp:ListItem>
<asp:ListItem Value="P3">Process 3</asp:ListItem></asp:DropDownList>
<br />
<br />
<asp:Label ID="lbl_statio" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style1">Tester Name</td>
<td class="auto-style2">:</td>
<td class="auto-style3">
<asp:TextBox ID="txt_testername" runat="server" AutoPostBack="true" OnTextChanged="TextBox_TextChanged" CssClass="txt-style"></asp:TextBox>
</td>
</tr>
</table>
</div>
<script>
function focustester() {
var process = document.getElementById('<%= ddl_processname.ClientID%>');
if (process.value != "") {
process.options[process.selectedIndex].setAttribute("style", "color:red;font-weight:600;");
process.options[process.selectedIndex].setAttribute("disabled", "disabled");
document.getElementById('<%= txt_testername.ClientID%>').focus();
}
}
</script>
</html>
ASP.NET代碼后面
protected void TextBox_TextChanged(object sender, EventArgs e)
{
TextBox txt_ID = (TextBox)sender;
string get_certain_box = "";
if (ddl_processname.SelectedValue.ToString() == "")
{
Response.Write(@"<script language=javascript>alert('Please select Process first...')</script>");
txt_ID.Text = "";
ddl_processname.Focus();
}
else
{
if (txt_ID.Text.ToString() == "")
{
get_certain_box = (txt_ID.ID.ToString() == "txt_testername") ? "Tester field" : "txtID";
Response.Write(@"<script language=javascript>alert('" + get_certain_box + " cannot blank...')</script>");
txt_ID.Focus();
}
else
{
if (txt_ID.ID.ToString() == "txt_testername")
{
Response.Write(@"<script language=javascript>alert('success')</script>");
}
}
}
}
我可能錯了,但我記得如果下拉菜單被禁用或其他原因,后面的代碼將無法獲取數據。 那么你可以在go到focustester()之后禁用下拉菜單嗎? 但是如果您堅持使用 JavaScript 那么為什么不使用 setAttribute 時將值插入隱藏字段之類的解決方法呢?
添加隱藏字段。 然后將下拉值分配給 javascript 中的隱藏字段。 在服務器端訪問下拉值的隱藏值。
<asp:HiddenField ID="hdnProcess" runat="server" />
Javascript
function focustester() {
var process = document.getElementById('<%= ddl_processname.ClientID%>');
if (process.value != "") {
process.options[process.selectedIndex].setAttribute("style", "color:red;font-weight:600;");
process.options[process.selectedIndex].setAttribute("disabled", "disabled");
var hdn = document.getElementById('<%=hdnProcess.ClientID%>');
hdn.value = process.value;
document.getElementById('<%= txt_testername.ClientID%>').focus();
}
}
服務器端
protected void txt_testername_TextChanged(object sender, EventArgs e)
{
{
TextBox txt_ID = (TextBox)sender;
string get_certain_box = "";
string ddlval = hdnProcess.Value.ToString();
if (ddlval == "")
{
Response.Write(@"<script language=javascript>alert('Please select Process first...')</script>");
txt_ID.Text = "";
ddl_processname.Focus();
}
else
{
if (txt_ID.Text.ToString() == "")
{
get_certain_box = (txt_ID.ID.ToString() == "txt_testername") ? "Tester field" : "txtID";
Response.Write(@"<script language=javascript>alert('" + get_certain_box + " cannot blank...')</script>");
txt_ID.Focus();
}
else
{
if (txt_ID.ID.ToString() == "txt_testername")
{
Response.Write(@"<script language=javascript>alert('success')</script>");
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.