dropdownlist1.Attributes.Add("OnSelectedIndexChanged", "return confirm('Are you sure to change this line');")
button1.Attributes.Add("onclick", "return confirm('delete this line');")
8 个解决方案
#1
你可以直接在OnSelectedIndexChanged这个事件的代码里写
#2
怎么写呢
#3
在事件处理中写上这个代码
#4
it is not as simple as you would think, play with the following code:
<form runat="server">
<asp:DropDownList id=DropDownList1 runat=server OnSelectedIndexChanged="TestSel" AutoPostBack="true">
<asp:ListItem Text="a" Value="0" />
<asp:ListItem Text="b" Value="1" />
<asp:ListItem Text="c" Value="2" />
<asp:ListItem Text="d" Value="3" />
</asp:DropDownList>
</form>
<script language="C#" runat="server">
void TestSel(Object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Response.Write(ddl.SelectedItem.Value);
}
</script>
<%=DateTime.Now%>
<script language="javascript">
var nOld = -1;
var fnOldChange = null;
function testConfirm()
{
return confirm('are you sure?');
}
function newChange()
{
var ret = testConfirm();
if (ret)
{
if(fnOldChange != null)
fnOldChange();
else
nOld = event.srcElement.selectedIndex;
}
else
{
if (nOld != -1)
event.srcElement.selectedIndex = nOld;
}
}
function window.onload()
{
var e = document.getElementById("DropDownList1");
fnOldChange = e.onchange;
e.onchange = newChange;
nOld = e.selectedIndex;
}
</script>
<form runat="server">
<asp:DropDownList id=DropDownList1 runat=server OnSelectedIndexChanged="TestSel" AutoPostBack="true">
<asp:ListItem Text="a" Value="0" />
<asp:ListItem Text="b" Value="1" />
<asp:ListItem Text="c" Value="2" />
<asp:ListItem Text="d" Value="3" />
</asp:DropDownList>
</form>
<script language="C#" runat="server">
void TestSel(Object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Response.Write(ddl.SelectedItem.Value);
}
</script>
<%=DateTime.Now%>
<script language="javascript">
var nOld = -1;
var fnOldChange = null;
function testConfirm()
{
return confirm('are you sure?');
}
function newChange()
{
var ret = testConfirm();
if (ret)
{
if(fnOldChange != null)
fnOldChange();
else
nOld = event.srcElement.selectedIndex;
}
else
{
if (nOld != -1)
event.srcElement.selectedIndex = nOld;
}
}
function window.onload()
{
var e = document.getElementById("DropDownList1");
fnOldChange = e.onchange;
e.onchange = newChange;
nOld = e.selectedIndex;
}
</script>
#5
行是行,不过能不能全在后台写呢?我的dropdownlist 是动态产生的,放在datagrid中,所以最好是全在codebehind中,不知怎么写,谢了 saucer
#6
而且,我在datagrid中会有多个dropdownlist,例如10个,那这句 var e = document.getElementById("DropDownList1"); 怎么改
#7
这段代码能运行,给你借鉴。
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
StringBuilder script=new StringBuilder();
script.Append("<script language=javascript>\n");
script.Append("function OnSelectChange()\n");
script.Append("{\n");
script.Append("alert('你的选择是:'+document.all.DropDownList1.options[document.all.DropDownList1.selectedIndex].text);\n");
script.Append("\n}\n</script>");
RegisterClientScriptBlock("OnSelectIndexChange",script.ToString());
DropDownList1.Attributes.Add("OnChange","OnSelectChange()");
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
StringBuilder script=new StringBuilder();
script.Append("<script language=javascript>\n");
script.Append("function OnSelectChange()\n");
script.Append("{\n");
script.Append("alert('你的选择是:'+document.all.DropDownList1.options[document.all.DropDownList1.selectedIndex].text);\n");
script.Append("\n}\n</script>");
RegisterClientScriptBlock("OnSelectIndexChange",script.ToString());
DropDownList1.Attributes.Add("OnChange","OnSelectChange()");
}
#8
for dropdownlists in a datagrid, you probably need to do the following (didn't test, so it might not work), of course, if you want to make them code behind, go ahead, it is not difficult:
<script language="javascript">
var nOldIndices = null;
var fnOldChange = null;
function testConfirm()
{
return confirm('are you sure?');
}
function newChange()
{
var ret = testConfirm();
//find the index, here is a dumb but simple way, you could get the TD and then TR and get its rowIndex
var e = event.srcElement;
var selects = document.all("YourDataGrid").all.tags("SELECT");
var i;
for (i=0; i < selects.length; i++)
{
if (selects[i] == e)
break;
}
var nIndex = i;
if (ret)
{
if(fnOldChange[nIndex] != null)
fnOldChange[nIndex]();
else
nOldIndices[nIndex] = e.selectedIndex;
}
else
{
if (nOldIndices[nIndex] != -1)
e.selectedIndex = nOldIndices[nIndex];
}
}
function window.onload()
{
var selects = document.all("YourDataGrid").all.tags("SELECT");
nOldIndices = new Array(selects.length);
fnOldChange = new Array(selects.length);
for(var i=0; i < nOldIndices.length; i++)
{
fnOldChange[i] = selects[i].onchange;
selects[i].onchange = newChange;
nOldIndices[i] = selects[i].selectedIndex;
}
}
</script>
<script language="javascript">
var nOldIndices = null;
var fnOldChange = null;
function testConfirm()
{
return confirm('are you sure?');
}
function newChange()
{
var ret = testConfirm();
//find the index, here is a dumb but simple way, you could get the TD and then TR and get its rowIndex
var e = event.srcElement;
var selects = document.all("YourDataGrid").all.tags("SELECT");
var i;
for (i=0; i < selects.length; i++)
{
if (selects[i] == e)
break;
}
var nIndex = i;
if (ret)
{
if(fnOldChange[nIndex] != null)
fnOldChange[nIndex]();
else
nOldIndices[nIndex] = e.selectedIndex;
}
else
{
if (nOldIndices[nIndex] != -1)
e.selectedIndex = nOldIndices[nIndex];
}
}
function window.onload()
{
var selects = document.all("YourDataGrid").all.tags("SELECT");
nOldIndices = new Array(selects.length);
fnOldChange = new Array(selects.length);
for(var i=0; i < nOldIndices.length; i++)
{
fnOldChange[i] = selects[i].onchange;
selects[i].onchange = newChange;
nOldIndices[i] = selects[i].selectedIndex;
}
}
</script>
#1
你可以直接在OnSelectedIndexChanged这个事件的代码里写
#2
怎么写呢
#3
在事件处理中写上这个代码
#4
it is not as simple as you would think, play with the following code:
<form runat="server">
<asp:DropDownList id=DropDownList1 runat=server OnSelectedIndexChanged="TestSel" AutoPostBack="true">
<asp:ListItem Text="a" Value="0" />
<asp:ListItem Text="b" Value="1" />
<asp:ListItem Text="c" Value="2" />
<asp:ListItem Text="d" Value="3" />
</asp:DropDownList>
</form>
<script language="C#" runat="server">
void TestSel(Object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Response.Write(ddl.SelectedItem.Value);
}
</script>
<%=DateTime.Now%>
<script language="javascript">
var nOld = -1;
var fnOldChange = null;
function testConfirm()
{
return confirm('are you sure?');
}
function newChange()
{
var ret = testConfirm();
if (ret)
{
if(fnOldChange != null)
fnOldChange();
else
nOld = event.srcElement.selectedIndex;
}
else
{
if (nOld != -1)
event.srcElement.selectedIndex = nOld;
}
}
function window.onload()
{
var e = document.getElementById("DropDownList1");
fnOldChange = e.onchange;
e.onchange = newChange;
nOld = e.selectedIndex;
}
</script>
<form runat="server">
<asp:DropDownList id=DropDownList1 runat=server OnSelectedIndexChanged="TestSel" AutoPostBack="true">
<asp:ListItem Text="a" Value="0" />
<asp:ListItem Text="b" Value="1" />
<asp:ListItem Text="c" Value="2" />
<asp:ListItem Text="d" Value="3" />
</asp:DropDownList>
</form>
<script language="C#" runat="server">
void TestSel(Object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Response.Write(ddl.SelectedItem.Value);
}
</script>
<%=DateTime.Now%>
<script language="javascript">
var nOld = -1;
var fnOldChange = null;
function testConfirm()
{
return confirm('are you sure?');
}
function newChange()
{
var ret = testConfirm();
if (ret)
{
if(fnOldChange != null)
fnOldChange();
else
nOld = event.srcElement.selectedIndex;
}
else
{
if (nOld != -1)
event.srcElement.selectedIndex = nOld;
}
}
function window.onload()
{
var e = document.getElementById("DropDownList1");
fnOldChange = e.onchange;
e.onchange = newChange;
nOld = e.selectedIndex;
}
</script>
#5
行是行,不过能不能全在后台写呢?我的dropdownlist 是动态产生的,放在datagrid中,所以最好是全在codebehind中,不知怎么写,谢了 saucer
#6
而且,我在datagrid中会有多个dropdownlist,例如10个,那这句 var e = document.getElementById("DropDownList1"); 怎么改
#7
这段代码能运行,给你借鉴。
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
StringBuilder script=new StringBuilder();
script.Append("<script language=javascript>\n");
script.Append("function OnSelectChange()\n");
script.Append("{\n");
script.Append("alert('你的选择是:'+document.all.DropDownList1.options[document.all.DropDownList1.selectedIndex].text);\n");
script.Append("\n}\n</script>");
RegisterClientScriptBlock("OnSelectIndexChange",script.ToString());
DropDownList1.Attributes.Add("OnChange","OnSelectChange()");
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
StringBuilder script=new StringBuilder();
script.Append("<script language=javascript>\n");
script.Append("function OnSelectChange()\n");
script.Append("{\n");
script.Append("alert('你的选择是:'+document.all.DropDownList1.options[document.all.DropDownList1.selectedIndex].text);\n");
script.Append("\n}\n</script>");
RegisterClientScriptBlock("OnSelectIndexChange",script.ToString());
DropDownList1.Attributes.Add("OnChange","OnSelectChange()");
}
#8
for dropdownlists in a datagrid, you probably need to do the following (didn't test, so it might not work), of course, if you want to make them code behind, go ahead, it is not difficult:
<script language="javascript">
var nOldIndices = null;
var fnOldChange = null;
function testConfirm()
{
return confirm('are you sure?');
}
function newChange()
{
var ret = testConfirm();
//find the index, here is a dumb but simple way, you could get the TD and then TR and get its rowIndex
var e = event.srcElement;
var selects = document.all("YourDataGrid").all.tags("SELECT");
var i;
for (i=0; i < selects.length; i++)
{
if (selects[i] == e)
break;
}
var nIndex = i;
if (ret)
{
if(fnOldChange[nIndex] != null)
fnOldChange[nIndex]();
else
nOldIndices[nIndex] = e.selectedIndex;
}
else
{
if (nOldIndices[nIndex] != -1)
e.selectedIndex = nOldIndices[nIndex];
}
}
function window.onload()
{
var selects = document.all("YourDataGrid").all.tags("SELECT");
nOldIndices = new Array(selects.length);
fnOldChange = new Array(selects.length);
for(var i=0; i < nOldIndices.length; i++)
{
fnOldChange[i] = selects[i].onchange;
selects[i].onchange = newChange;
nOldIndices[i] = selects[i].selectedIndex;
}
}
</script>
<script language="javascript">
var nOldIndices = null;
var fnOldChange = null;
function testConfirm()
{
return confirm('are you sure?');
}
function newChange()
{
var ret = testConfirm();
//find the index, here is a dumb but simple way, you could get the TD and then TR and get its rowIndex
var e = event.srcElement;
var selects = document.all("YourDataGrid").all.tags("SELECT");
var i;
for (i=0; i < selects.length; i++)
{
if (selects[i] == e)
break;
}
var nIndex = i;
if (ret)
{
if(fnOldChange[nIndex] != null)
fnOldChange[nIndex]();
else
nOldIndices[nIndex] = e.selectedIndex;
}
else
{
if (nOldIndices[nIndex] != -1)
e.selectedIndex = nOldIndices[nIndex];
}
}
function window.onload()
{
var selects = document.all("YourDataGrid").all.tags("SELECT");
nOldIndices = new Array(selects.length);
fnOldChange = new Array(selects.length);
for(var i=0; i < nOldIndices.length; i++)
{
fnOldChange[i] = selects[i].onchange;
selects[i].onchange = newChange;
nOldIndices[i] = selects[i].selectedIndex;
}
}
</script>