Solved

How to get other control value at grid view?

Posted on 2007-12-05
10
220 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

930 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

11 Experts available now in Live!

Get 1:1 Help Now