组合框上的Javascript代码所选索引已更改 [英] Javascript code on combo box selected index changed

查看:63
本文介绍了组合框上的Javascript代码所选索引已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我必须根据从数据库中获取的值来显示一个警报框,例如在我的情况下,其中有一个网格,其中有一个组合框.

关于所选索引更改事件,我正在从数据库中获取值.根据获取的值(如果我的条件为true),则只有我必须向用户显示警报框(通过Java脚本),否则就不会.并且还希望同时取消选择选定的项目.

我无法获得正确的代码.请帮忙.

谢谢

Varun Sareen

Dear Friends,

I have to show an alert box on the basis of the value that i am getting from the database like in my case there is a grid in which there is a combo box.

On the selected index changed event of which I am fetching a value from the database. On the basis of fetched value if my condition is true then only i have to show an alert box (through java script) to the user else not. And also want to deselect the selected item at the same time.

I am not able to get the correct code for doing so. Please help.

Thanks

Varun Sareen

推荐答案

Such scenario can be handled in many ways. Select appt as per your requirement.
Assuming a DropDown is inside a Grid.
I.
Grid declaration (aspx):
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound"

            >
            <Columns>
                <asp:TemplateField HeaderText="Combo">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"

                            AutoPostBack="True"

                            onselectedindexchanged="DropDownList1_SelectedIndexChanged1">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Javascript function in aspx:
 function doAlertClientID(client) {
            var obj = document.getElementById(client);
            if (obj.options[obj.selectedIndex].value == "Alert") {
                obj.selectedIndex = 0;
                alert("Alert");
            }
        }

In Code behind:
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DropDownList list = sender as DropDownList;
        ClientScript.RegisterStartupScript(GetType(), "Alert", "doAlertClientID('" + list.ClientID + "')", true);
        // Or you can make any database transaction and call above method conditionally
    }

II.
Grid delaration (aspx):
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound"

            >
            <Columns>
                <asp:TemplateField HeaderText="Combo">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" onchange="doAlert(this);">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Javascript function in aspx:
function doAlert(obj) {
            if (obj.options[obj.selectedIndex].value == "Alert") {
                obj.selectedIndex = 0;
                alert("Alert");
            }
        }

These examples are not currently with required exceptional handling. Modify them as per valid data.

Also if Update Panel is involved then ensure to use ScriptManager for invoking javascript in (I) example.


您可以获取值和使用以下代码显示下拉列表的文本.

You can get the value and the Text of the dropdownlist using the following code.

var ddlReport = document.getElementById("<%=DropDownListReports.ClientID%>");
var Text = ddlReport.options[ddlReport.selectedIndex].text;
var Value = ddlReport.options[ddlReport.selectedIndex].value;



现在,根据您的业务逻辑调整条件.

如果这对您真的很有帮助,请不要忘记投票并接受.随时回复以获取更多帮助



Now put your condition according to your business logic.

If this would be really helpful to you then don''t forgot to Vote and Make Answer as Accepted. And feel free to reply for further help


这篇关于组合框上的Javascript代码所选索引已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆