GridView DropDownList events

D

dev648237923

When editing I show a DropDownList.
I want to show/hide a textbox next to that list if the user selects Other.

I've tried many ways but I can get it to work:
1.) If I knew the TextBox controls name/id I could just add an onchange
attribute to my DDL during RowDataBound and then put in some javascript to
show/hide the textbox but I never know in advance the name/id of the textbox
since it changes depending on what row I am on.
2.) I could set the DDL to AutoPostBack but then how would I affect the
textBox in that row?

Thanks for any ideas!
 
S

Steven Cheng[MSFT]

Hello dev648237923,

Regarding on the dropdownlist and textbox interact requirement, I think you
can use the GridView.RowCreated event to set script handler for the
DropDownList( the "onchange" client event). And at that time, you can get
the Textbox instance also and embed its "ClientID"(which represent its id
at client-side html) into your script function call. For example, suppose
you have a TemplateField in gridview as below:

==============================
.......................
<asp:TemplateField HeaderText="template column">
<ItemTemplate >
<asp:DropDownList ID="lstTest" runat="server" >
<asp:ListItem Text="aaa" Value="aaa"></asp:ListItem>
<asp:ListItem Text="bbb" Value="bbb"></asp:ListItem>
<asp:ListItem Text="ccc" Value="ccc"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtTest" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

=======================

In GridView's RowCreated event, you can find the DropDownList and Textbox
and do some customization on them:

=======================
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.RowState == DataControlRowState.Normal)
{
DropDownList lst = e.Row.FindControl("lstTest") as
DropDownList;
TextBox txt = e.Row.FindControl("txtTest") as TextBox;

lst.Attributes["onchange"] =
"javascript:AdjustTextBox(this, '" + txt.ClientID + "');";

}
}
==================

Here is the client script functino
=============
<script type="text/javascript">

function AdjustTextBox(lst, txtid)
{
var txt = document.getElementById(txtid);

txt.value = lst.selectedIndex;
}
</script>

================

You can adjust the test code to fit your specific requirement. If you have
any further questions on this, please feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top