Solved

How to get other control value at grid view?

Posted on 2007-12-05
10
223 Views
Last Modified: 2008-02-01
Hi,

I have a gridview which have several controls which is bind to a datatable.

The grid have dropdownlist and textbox. When the textbox is fill with something then on textchanged I want to know what is the dropdownlist selected value.

Please help.

Thank you.







0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 11

Expert Comment

by:Robb Hill
ID: 20414403
My first question is what is the order of events on the page then I can give you a solution.  This is what I am assuming, please let me know if my assumptions are correct.

Your gridview loads.  You have ddlist and tbox on every line in gview.  a user types on a row a value in a text box.  After typing and leaving the text box you want to know the selected value of the dropdown list in that row of the gridview?
0
 
LVL 11

Expert Comment

by:Robb Hill
ID: 20414459
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)

        Dim index As Integer = GridView1.EditIndex
        Dim row As GridViewRow = GridView1.Rows(index)
       
        Dim dropdownlistobjectvariableAM As DropDownList = CType(row.FindControl("YourDropDownList"), DropDownList)
     
        e.NewValues("your_field") = dropdownlistobjectvariableAM.SelectedValue
0
 
LVL 16

Accepted Solution

by:
GreymanMSC earned 500 total points
ID: 20416435
Make sure the textbox's AutoPostBack property is set to "True".   This makes the TextChanged event fire.

Then in the TextChanged event, find the dropdown list in the textbox's parent's controls collection.  This will locate the dropdownlist in the same row of the gridview.
Protected Sub TheTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '<!-- locate the drop down list relative to the sender -->
    Dim T As TextBox = CType(sender, TextBox)
    Dim D As DropDownList = T.Parent.FindControl("TheDropDownList")
    If Not D Is Nothing Then
        '<!-- do stuff with it... -->
        Dim Okay As Boolean = False
        For Each Li As ListItem In D.Items
            If Li.Text.Trim.Equals(T.Text.Trim) Then
                D.SelectedValue = Li.Value
                T.Text = Li.Text
                Okay = True
            End If
        Next
        If Not Okay Then
            T.Text = D.SelectedItem.Text
        End If
    End If
End Sub

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Author Comment

by:emi_sastra
ID: 20416745
Hi Rob,

I don't set autopostback = "true", so it doesn't fired GridView1_RowUpdating event.
Actually I don't want to use autoposback.

I use txtQty_TextChanged(ByVal sender As Object, ByVal e As EventArgs), this event fired even there no autoposback='true" is set.

Thank you
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20416785
Hi Grey,

First, your code has weakness, what would happened if the textbox value are all (some)  the same for all rows. In that case the code will get wrong value.

Second, there is easy way, just get the grid row, then get the dropdownlist selected value of the row.
The problem is I don't know how to get it in code.

Thank you.

0
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 20417016
The value of the textbox is irrelevant to identifying the textbox.  The code always uses the textbox(s) that had triggered of the OnTextChange event.  It's value will be whatever it was to which was changed.  
 
The first two lines of the code identifies this sender and matches it to the drop down list that is in the same row.  What stuff you do with their content after that is up to you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20417370
Sorry,

What is the usage of this code :

If Li.Text.Trim.Equals(T.Text.Trim) Then

Dim T As TextBox = CType(sender, TextBox)

Let say T="X"

What would be the Li.Text. ?
What would be the Li.Value?.

Why should be equal:  If Li.Text.Trim.Equals(T.Text.Trim) Then  to get the d.value?

Why set T.Text = Li.Text when Li.Text.Trim.Equals(T.Text.Trim) ?

I rather confuse here.

Thank you.
0
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 20417486
It's just examples of how to access the information in the dropdownlist, since you didn't specify what you wanted to do with it after the textbox was updated.

So, what exactly do you want to do?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20417507
Hi Rob,

I set autoposback ="true", but Protected Sub grid_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grid.RowUpdating

not fired.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20417645
Hi Greyman,

At last, I just add:

Dim strValue As String = D.SelectedValue

After below code

 Dim T As TextBox = CType(sender, TextBox)
    Dim D As DropDownList = T.Parent.FindControl("TheDropDownList")

And it solve my problem.

The complete code will be:

 Dim T As TextBox = CType(sender, TextBox)
    Dim D As DropDownList = T.Parent.FindControl("TheDropDownList")

Dim strValue As String = D.SelectedValue

The strValue then contain the value inside the TheDropDownList.

Sorry, I just confuse of the iteration, and still now.

Thank you very much anyway.





0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question