Auto Ad Code

Showing posts with label asp. Show all posts
Showing posts with label asp. Show all posts

Tuesday, December 4, 2012

[SOLVED] Dropdownlist SelectedIndexChanged (OnSelectedIndexChanged) From GridView

You may want to implement some functionality on changing index of a drop down list which is provided within a grid for multi records. I too had to do the same and searched over internet for some suitable solution. Though there are more than one solutions but the solution I am providing is most easy of them and requires no tricks.
The Solution
Note the RED BOLD code of HTML view, which means you will have to set EnableViewState="false" for the griview and also you will have to set AutoPostBack="true" for the dropdownlist.

Here is your GridView code (The HTML)


<asp:GridView runat="server" ID="gvTest" Width="50%" AutoGenerateColumns="false" EnableViewState="false">
            <Columns>
                <asp:BoundField DataField="Gender" HeaderText="Gender" HeaderStyle-HorizontalAlign="Left" />
                <asp:TemplateField >
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlTest" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
                            <asp:ListItem Value="Male">Male</asp:ListItem>
                            <asp:ListItem Value="Female">Female</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


And here is Code behind 
1. C#

protected void Page_Load(object sender, System.EventArgs e)
{
       try {
              FillGridView();
       } catch (Exception ex) {
              Response.Write(ex.Message);
       }
}

private void FillGridView()
{
       try {
              DataTable dt = new DataTable();
              dt.Columns.Add("Gender");
              DataRow dr = null;
              dr = dt.NewRow();
              dr["Gender"] = "I am a ";
              dt.Rows.Add(dr);

              gvTest.DataSource = dt;
              gvTest.DataBind();
       } catch (Exception ex) {
              Response.Write(ex.Message);
       }
}

protected void ddlTest_SelectedIndexChanged(object sender, System.EventArgs e)
{
       try {
              Response.Write("You selected " + ((DropDownList)sender).SelectedItem.Value);
       } catch (Exception ex) {
              Response.Write(ex.Message);
       }
}



2. VB.Net

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            FillGridView()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub FillGridView()
        Try
            Dim dt As New DataTable
            dt.Columns.Add("Gender")
            Dim dr As DataRow
            dr = dt.NewRow
            dr("Gender") = "I am a "
            dt.Rows.Add(dr)

            gvTest.DataSource = dt
            gvTest.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Protected Sub ddlTest_SelectedIndexChanged(sender As Object, e As System.EventArgs)
        Try
            Response.Write("You selected " & CType(sender, DropDownList).SelectedItem.Value)
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub


Monday, July 12, 2010

Default Button (Enter Key)

Many of us faces the issue that while typing in a textbox we press Enter and expect a button named "A" to be clicked but this doesn't happens and button "B" gets clicked.
For example you have provided a Textbox for Search and a button is given with that textbox so that when that button will be clicked your code will try to search in database for whatever written in Textbox. You expect that when user will press enter while typing in Textbox then search button will be clicked by default but this doesn't happen and instead soneother button (like login etc) get clicked. Pretty much annoyiong is this and even tab sequence don't work here.
Whats the solution?
Its pretty much easy which is to add yout HTML code for that textbox and button withing a <asp:Panel. There is a property named "DefaultButton" for <asp:Panel. You can write ID of your button in the "DefaultButton" property of <asp:Panel. Now whenever Enter key will be pressed in any textbox contained in that <asp:Panel then that button will be clicked whose ID is given against "DefaultButton" property of <asp:Panel.

Sample code
<asp:Panel runat="server" ID="pnlChild" DefaultButton="ButtonID">
    YOUR HTML CODE Including the Button to be Clicked
</asp:Panel>