Find edited textbox value inside DataItemTemplate on Button click event - devexpress

I want to get the edited value in the textbox on a button click event. I have tried this code
string Value = gvSample.GetRowValues(i, "Word").ToString();
but it gives me the old value of the textbox.
I also tried
TextBox txtTrans = (TextBox)gvSample.FindControl("txtSample");
string Value = txtTrans.Text;
but txtTrans always returns NULL.
Please help.

Related

Calling Code Behind Function and passing arguments

I have asp.net text box where I am calling the code behind function through __postback but I also want to pass text of the text box which is calling __postback function.
Here is my text box.
<asp:TextBox Width="400px" ID="txtExtraRooms" MaxLength="3"
onkeypress="__doPostBack(this.name,'OnKeyPress');" runat="server"
Visible="false"></asp:TextBox>
And my code behind where I want to get entered text of text box with id="txtExtraRooms"
Dim ctrlName As String = Request.Params(Page.postEventSourceID)
Dim args As String = Request.Params(Page.postEventArgumentID)
// here I want text of text box with id "txtExtraRooms"
If ((ctrlName = txtExtraRooms.UniqueID) _
AndAlso (args = "OnKeyPress")) Then
TextBox2_OnKeyPress(ctrlName, args)
End If
The delegate for any asp.net event has the 1st argument as the "sender" which holds the reference to the control raising the event.
For textbox, you should use the TextChanged event rather than doing the postback (which will require state management as well).
Add an AutoPostback=true on textbox and provide the TextChanged=handler. The page postback will happen, although handling will be on the provided handler.
Also, by using the "keypress" event it will fire for each key typed (entered or deleted). Rather the event should fire when user has completed providing the input and has moved the focus to another control.
In asp.net there are 2 main fields that you can check in the form object which are
__EVENTTARGET: The Control Which raised the event for postback
__EVENTARGUMENT: Arguments Passed
and to read them you can use :
Dim eventArgument As String = IIf(Me.Request("__EVENTARGUMENT") = Nothing,
String.Empty, Me.Request("__EVENTARGUMENT"))
for more details you can check
https://forums.asp.net/t/1252181.aspx?Retreiving+Argument+from+_doPostBack+in+VB

Update Label control In first record of DataList during ItemDataBound

Using Asp.net and VB.net. I have a DataList on a webpage. The datalist has a label control. I want to update the text of the label control in the first record with information obtained from the subsequent records as those subsequent records are databound. In other words, each time the datalist gets bound, I want to identify the label in the first record and then update the text of that label. I'm attempting to do this in the ItemDataBound by getting the ClientID of the label in the first record:
Dim strMealPrice As String = CType(e.Item.FindControl("lblMealPrice"), Label).ClientID
and then hold that ClientID in a hidden label outside of the datalist.
If lblhidMealHeaderID.Text = "" then
lblhidMealHeaderID.Text = strMealPrice
End if
Everything works up to this point.
Then each time the datalist ItemDataBound is fired I use findcontrol to try to update the label in the first record but I'm unsure how to format the findcontrol when using a variable for the label control's ClientID (lblhidMealHeaderID.text). But even when I hard code the ClientID of the label in the first record I can't get it to work.
Dim tempLabel As Label = DataList1.FindControl("DataList1_ctl00_lblMealPrice")
or
Dim tempLabel As Label = CType(e.Item.FindControl("DataList1_ctl00_lblMealPrice"), Label)
I get a Object reference not set to an instance of an object. when I try to write to tempLabel.
As you can see I'm grasping here. First, is this the best way to do this - is the ItemDataBound where I should be attempting this? Perhaps you can't update previous records while the DataList is "binding" subsequent records. Second, is ClientID the way to do this - I see ClientID is used mostly for javascript? Third, how do I properly format the FindControl using ClientID?
Any and all help is greatly appreciated.
In ItemDataBound use this
If e.Item.ItemIndex = 0 Then
CType(e.Item.FindControl("lblMealPrice"), Label).Text = strMealPrice
End If
Update
You can find the first label any time after binding by looping through its items.
For Each item as DataGridItem In dgGrid.Items
CType(item.FindControl("lblMealPrice"), Label).Text = strMealPrice
Next

Programmatically change dropdownlist selected value

I want to create an application that is able to change the selected value of the dropdownlist in the web page.
I tried to use the code below to change the selected value of the dropdownlist:
Dim TypeSelection As HtmlElement = WebBrowser1.Document.GetElementById("dropdownlist1")
Dim TypeSelectionOption As HtmlElement = TypeSelection.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.GetAttribute("value") = "ABC")
TypeSelectionOption.SetAttribute("selected", "true")
TypeSelection.InvokeMember("onchange")
The selected value of the dropdownlist had been changed, but it can't post back to the server, it just hangs at there. I tried to create a testing page to test my code, the code is working & able to post back to the server.
Is there any other method to change the selected value of the dropdownlist, that is able to post back to the server?

validate a template text box with database value

I'm developing a website and have a textbox in gridview template field. This textbox collects the input for the "order quantity" which has to be compared against the "available quantity"(stored in the database).
I use RowEditing, RowUpdating and RowCanceling events for edit operations in gridview.
I tried implementing the above requirement with "TextChanged" Event of text box. But the issue is, when the user edits the quantity and clicks on "Update" button, text box event fires first and update button event doesn't fire. So, user is forced to click the "Update" button again.
Is there any work around for the above scenario or any other event used to implement above requirement?
Thanks,
Balaji g
You can Use RangeValidator For That...
<asp:RangeValidator ID="RangeValidator1" ControlToValidate="YourTexBoxId"
runat="server" MinimumValue="0" Type="Integer"
MaximumValue='<%# GetQuantity(Eval(TotalQaunt).ToString(),Eval(ItemId).ToString()) %>'
ErrorMessage="RangeValidator"></asp:RangeValidator>
write a function lyk this ON CODE BEHIND
string GetQuantity(string totalquantity,string itemid)
{
DO OPERATRION TO GET QUANTITY.....
return quantity;
}
I think I'd try the CustomValidator. With the CustomValidator, you can define the validation code in the "_ServerValidate" event handler.
In your validation code, you can call the database to get the current value and then compare it to the submitted value (which would be available via the event handler's ServerValidateEventArgs paramenter if you specify a ControlToValidate).
In order to look up the current value in the database, you'll need to know which record was clicked. For that, you could either use the row button's CommandArgument property or include a hidden field in your row to store some type of ID. Then you could capture that ID and use it to look up the desired record in the database.
Updated with Code
Protected Sub cusCustom_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim cv As CustomValidator = CType(source, CustomValidator)
Dim HiddenField_ID As HiddenField = CType(cv.Parent.FindControl("HiddenField_ID"), HiddenField)
*** Use ID to look up value in database ***
End Sub

Issue binding a DataTable to an ASP.Net GridView control

I'm trying to allow my users to enter in large lists of data using an ASP.Net GridView control. The affect I'm trying to create is to make the GridView control act like a spreadsheet. The user can freely enter data and tab from column to column and row to row. The user can use a button at the bottom of the page to add rows as needed. There is also a button at the bottom of the form to save as needed.
To do this, I created a DataTable with a bunch of empty rows and bound it to a GridView. The GridView's columns are template columns that contain textboxes. So, when the page opens, it actually looks like a spread sheet. When the user hits the add rows button, I just add another ten rows to the DataTable the GridView is bound to and it works like a charm.
The issue I'm running into is reading the data that the user entered. When the user hits the paging link or the update button, I would like to update the DataTable with the data the user typed in. Here is what I have.
Private Sub UpdateDataTable()
Dim objCatRow As clsCategoriesRow = Session("gvCategoriesRow")
Dim drQuery() As DataRow = Nothing
Dim drRow As DataRow = Nothing
Dim objRow As GridViewRow = Nothing
Dim intRecNo As Integer = 0
Dim txt As TextBox = Nothing
Dim lbl As Label = Nothing
'Loop through all of the rows in the grid view control
For Each objRow In Me.gvCategories.Rows
'Get the label that contains the identity column
lbl = objRow.Cells(GridColumns.Category).FindControl("lblItemRecNo")
intRecNo = lbl.Text
'Update the datarow bound to this grid view row
'First, query the datarow from the data table
drQuery = objCatRow.Table.Select("recno = " & intRecNo)
'Make sure our query returned a row
If Not IsNothing(drQuery) AndAlso drQuery.Count > 0 Then
'Get the value from the textbox in the grid view
txt = objRow.Cells(GridColumns.Category).FindControl("txtItemCategory")
'Upadte the data row with the value the user entered
'THE VALUE IN txt.Text IS EMPTY. HOW CAN I GET THE VALUE THE USER TYPED IN?
drQuery(0)("Category") = txt.Text
'Get the value from the textbox in the grid view
txt = objRow.Cells(GridColumns.SortORder).FindControl("txtItemSortOrder")
'Upadte the data row with the value the user entered
drQuery(0)("sortorder") = txt.Text
End If
Next
End Sub
The issue is that this is not returning what the user typed in. The line
txt = objRow.Cells(GridColumns.Category).FindControl("txtItemCategory")
returns a reference to the textbox in the templated column. But, it contains the previous value, the value from the view state, not the value the user typed in.
How can I get the value the user typed into the grid?
I want to mention that I know how to add EDIT and UPDATE buttons to each row. I would like to avoid doing that way if I can. My users have huge lists of data to enter in and that approach would make the application unusable.
Thanks in advance,
Mike
The form data posted by the user is found in the Page.Request.Form.Item collection. The Page.Request.Form.AllKeys lists the "keys" associated with all of the form item values.
If Page.Request.Form.HasKeys Then
For Each key as String In Page.Request.Form.AllKeys
' step through the keys and use Page.Request.Form.Item(key) to get the data entered
Next
end If
After testing, I was able to get the data from the Request.Form data or the GridView control during page.load on postback, as long as you don't bind the control on postback, but only during the initial request ("GET").
Keep in mind, controls have to be re-created for each request to the page. The Request data posted is used by ASP.NET to repopulate form data controls only after the controls are re-created on the page and the ViewState for the controls is processed, etc.
Request.Form collection
ASP.NET Page Life Cycle
You should replace this:
txt = objRow.Cells(GridColumns.Category).FindControl("txtItemCategory")
with this
txt = objRow.Cells(GridColumns.Category).FindControl("txtItemCategory").Value

Resources