简体   繁体   中英

ASPx.Net How to catch a change in value in an Itemtemplate DDL

I'm struggling to find a way to solve this. I have a gridview and the first column of it is a DropDownList defined in an Itemtemplate:

            <asp:GridView ID="gvXYZ" runat="server" DataKeyNames="Serial, XYZValue"> 
                <Columns>    
                 <asp:TemplateField HeaderText="Status">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlStat" runat="server" OnSelectedIndexChanged="ddlStat_SelectedIndexChanged"><asp:ListItem>  </asp:ListItem><asp:ListItem>  </asp:ListItem><asp:ListItem>OK</asp:ListItem><asp:ListItem>NG</asp:ListItem></asp:DropDownList>
                        </ItemTemplate>
                        <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
                    </asp:TemplateField>
                    </Columns>   
            </asp:GridView>

The user is presented with an empty choice, OK or NG as possible selections. How can I trigger either JavaScript or VB side to run a function when the user makes any selection in any of the DDL in the grid? In order to complete what asked, the user has to have either OK or NG selected. I'm trying to tie the Save button to the fact that the grid has been completed. I know I can run JavaScript on a HTML dropdown so I tried to create a function to do it and from an article I found I tried to run a VB method from a javascript function:

        function ddlStat_SelectedIndexChanged() {
            var someValueToPass = 'Hello server';

            __doPostBack('CustomPostBack', someValueToPass);
        }

the script never runs, the postback does not occur and the VB side code:

Protected Sub ddlStat_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim x As Integer
    For x = 1 To 10 : x = x + 1 : Next
End Sub

is never triggered either. I thought to do this on the VB side because I think I can more easily access properties of the grid, like the number of rows so I can check the DDL in each one of them. Thank you for this and ask questions if I wasn't clear.

Setting AutoPostback property of the drop down should raise a post back call to the server side. JS function can be called using the HTML event onchange

<asp:DropDownList ID="MyDropDown" runat="server" AutoPostBack="True" 
         OnSelectedIndexChanged="SelectedChange" onchange="YourChangeFun(this);">
</asp:DropDownList>

Javascript:

<script type="text/javascript">
      function YourChangeFun(ddl)
      {
         alert(ddl.selectedIndex);
      }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM