Solved

How to get other control value at grid view?

Posted on 2007-12-05
10
219 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
  • 5
  • 3
  • 2
10 Comments
 
LVL 11

Expert Comment

by:Robb Hill
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 16

Expert Comment

by:GreymanMSC
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now