Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 24-Feb-11 2:17am
v2
Comments
Sunasara Imdadhusen 24-Feb-11 8:35am    
Are you getting any error?
Sandeep Mewara 24-Feb-11 9:44am    
On the selected index changed event of which I am fetching a value from the database
AJAX call, right?

XML
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.
 
Share this answer
 
v2
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900