Click here to Skip to main content
14,932,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi,

I have an Grid with {txt_prodname,txt_type,txt_pages,txt_copies} //here txt_type is my Dropdown list.

Now my requirement is

1. While focus on my dropdown i need to select an default value of "SelectedIndex=1" not "0".

hence i use js as
C#
function selectOneside(val) { //here val is my dropdown ID
var Dval = val.selectedIndex;
if (Dval == "0") {
    val.selectedIndex=1;
}
}

My dropdown inside grid is like..
XML
<ItemTemplate>
                    <asp:DropDownList ID="txt_type" runat="server" AutoPostBack="true" onfocus="selectOneside(this);" onselectedindexchanged="txt_type_SelectedIndexChanged">
                        <asp:ListItem Value="0">--Select Type--</asp:ListItem>
                    <asp:ListItem Value="1">One Side</asp:ListItem>
                    <asp:ListItem Value="2">Diff Front &amp; Back</asp:ListItem>
                    <asp:ListItem Value="3">Same Front &amp; Back</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>

Till that works fine, but while i go to the next row again its changed, without enter txt_prodname

Hence i use onblur event for my txt_prodname inside grid as...
XML
<ItemTemplate>
                    <asp:TextBox ID="txt_prodname" runat="server" Width="110px" OnTextChanged="txt_prodname_TextChanged" CssClass="autocompleteTXT"></asp:TextBox>
                </ItemTemplate>

My js is...
C#
function prodname(txtval,ddlval) {
    var Dval = txtval.value;
    if (Dval == "") {
        ddlval.selectedIndex = 0;
        txtval.focus();
    }
}

I bind this blur event from my server side as
txtprodname.Attributes.Add("onblur", "javascript : prodname(" + txtprodname.ClientID + "," + txttype.ClientID + ")");

As i mentioned in js(prodname) i got focus to my textbox but...
Problem is my dropdown not getback to "SelectedIndex=0" when txt_prodname is empty

focus and Blur Play in my life :(

Please help me out...
Posted
Updated 7-Jan-12 5:09am
v4
Comments
Kethu Sasikanth 7-Jan-12 9:08am
   
check if txt_prodname_TextChanged event is causing the problem
J.Karthick 7-Jan-12 11:08am
   
No it doesn't....
incaunu 10-Jan-12 3:34am
   
What are you doing in txt_type_SelectedIndexChanged callback ? Try to put the AutoPostBack property of the txt_type to false, to check if the Javascript is working properly.
J.Karthick 11-Jan-12 3:08am
   
yes...I try that but no use. My "txt_type.selectedIndex" remains "1"

I even try put alert('Curent Index'); I displays current Index of txt_type correctly. But not change it to "0" index again.

1 solution

A different way of thinking make me solve this....

I have changed my JS function as...

C#
function selectOneside(val) {
    var Dval = val.selectedIndex;
    var row = val.parentNode.parentNode;
    var rowIndex = row.rowIndex;

    rowIndex = rowIndex + 1;

    var newId = "";
    if (rowIndex.toString().length == 1) {
        newId = "0" + rowIndex.toString();
    }
    else {
        newId = rowIndex.toString();
    }

    var txt = document.getElementById("ctl00_Adminmaster_Grid_job_ctl" + newId + "_txt_prodname").value;

if (Dval == "0" && txt != "") {
    val.selectedIndex = 1;
}

}
   

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