任何人都知道它是如何導致 ASP.NET 代碼隱藏無法獲取所選值的。
我發現問題是javascript函式;
設定屬性部分:
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>");
}
}
}
}
uj5u.com熱心網友回復:
我可能錯了,但我記得如果下拉串列被禁用或其他原因,后面的代碼將無法獲取資料。那么你可以在它轉到 focustester() 后禁用下拉選單嗎?但是,如果您堅持使用 JavaScript,那么為什么不使用 setAttribute 時將值插入隱藏欄位之類的解決方法呢?
uj5u.com熱心網友回復:
添加隱藏欄位。然后將下拉值分配給 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>");
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/381690.html
標籤:javascript C# 网站
