How to pass the querystring parameter into FormControl in vb.net - vb.net

I am passing a value from one form to the other. The second form in inserting the data field into the table. How do I pass the ID(strPID) passed from the first form into the form control PatronIDTextBox of the second form?
VB.Code: on Page load -- strPID = Request.QueryString("value1")
Form control:
<InsertItemTemplate>
PatronID:
<asp:TextBox ID="PatronIDTextBox" runat="server" Text='<%# Bind("PatronID") %>' />
Thanks
Ok, Now I am trying to say it this way in object datasource so I can send it into insert statement without being displayed on the screen.
<Insert Parameters>
<asp:QueryStringParameter Name="PatronID" Type="int32" QueryStringField="value1" />
<asp:QueryStringParameter Name="PatronName" Type="String" QueryStringField="value2" />
</InsertParameter>
InsertCommand="INSERT INTO [tblIncident] ([PatronID], [PatronName]) values (#PatronID, #PatronName)

Have you tried directly putting the value into the textbox? Like this:
<asp:TextBox ID="PatronIDTextBox" runat="server" Text='<%= Request.QueryString("Value1") %>' />
Just be careful, because this approach could allow a XSS attack. You might want to read-up on those.

I deleted the text box for both id and name and instead I modified the objectdatasource Insert statement as follows. The value of value1 and value2 is captured on page load event.
<InsertParameters>
<asp:QueryStringParameter Name="PatronID" Type="Int32" QueryStringField="value1" />
<asp:QueryStringParameter Name="PatronName" Type="String" QueryStringField="value2"/>
</InsertParameters>

Related

Callback error: Specific cast is not valid

I'm using a devexpress editform on my visual basic project. I ask for user input on a ASPxGridView, one of the values is called, Start Date which is declare on the grid as:
<dx:GridViewDataDateColumn Caption="Start Date" FieldName="StartDate" HeaderStyle-Wrap="true" UnboundType="String" EditFormSettings-VisibleIndex="1" >
<PropertiesDateEdit DisplayFormatString="D" EditFormatString="D" ValidationSettings-RequiredField-IsRequired ="true"></PropertiesDateEdit>
</dx:GridViewDataDateColumn>
And the datasource looks like this:
<asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SpEdPortalConnectionString %>"
SelectCommand="ProcedureName" SelectCommandType="StoredProcedure"
UpdateCommand="ProcedureName" UpdateCommandType="StoredProcedure"
InsertCommand="ProcedureName" InsertCommandType="StoredProcedure">
<SelectParameters>
***
</SelectParameters>
<UpdateParameters>
***
</UpdateParameters>
<InsertParameters>
***
<asp:Parameter Name="StartDate" Type="String" />
***
</InsertParameters>
</asp:SqlDataSource>
Problem: Even if I actually select a datevalue for the StartDate the value inserted on the DB is null.
Note:The callback error only shows on the deployed server not on local environment.But on local it inserts null so seems like the problem is there too but is not throwing an exeption.
I could not find the cause but the problem was solve by adding an EditTemplate tag and manually binding the value as follows:
<dx:GridViewDataDateColumn Caption="Start Date" FieldName="StartDate" HeaderStyle-Wrap="true" UnboundType="String" EditFormSettings-VisibleIndex="1" >
<PropertiesDateEdit DisplayFormatString="D" EditFormatString="D" ValidationSettings-RequiredField-IsRequired ="true"></PropertiesDateEdit>
<EditItemTemplate>
<dx:ASPxDateEdit ID="dteSD" runat="server" ClientInstanceName="dteSD"
Date='<%# Bind("StartDate") %>'>
</dx:ASPxDateEdit>
</EditItemTemplate>
</dx:GridViewDataDateColumn>
If the format changes you can just add Theme property inside the dx:ASPxDateEdit tag

Find Radiobutton Control from ID String in vb.net

I have 3 radiobuttons in my aspx:
<asp:RadioButton ID="rdoMasculin"
runat="server" AutoPostBack="true"
Checked="true" GroupName="gender"
TextAlign="Right" />
<asp:RadioButton ID="rdoFeminin"
runat="server" AutoPostBack="true"
GroupName="gender" />
<asp:RadioButton ID="rdoAnonymous"
runat="server" AutoPostBack="true"
GroupName="gender" />
When sending the form I'm storing the ID of the checked radiobutton in a Session variable:
for example: Session("currentGender") = rdoZukuTermine.ID
When I'm getting back on the page in a later stage I'm using the following code in codebehind:
Dim currentRadio = CType(FindControl(Session("currentGender")), RadioButton)
currentRadio.Checked = True
Session.Remove("currentGender")
currentRadio ends up as a null reference. But when I'm checking the Session it contains the right ID as a string.
Can someone help me?
Is the RadioButton control directly in your .aspx or does it have any parent controls? if yes call Findcontrol() on it direct parent like this:
Dim currentRadio = CType(ParentControl.FindControl(Session("currentGender")), RadioButton)

how to validate a textbox length in vb.net?

In VB.NET , I am trying to validate a
<asp:textbox id="name" runat=server> </asp:textbox>
textbox value which should not exceed more than 200 characters and the textbox takes only string.
Which validator will be a good option ? i want to use custom validator, will it work with string values?
<asp:RegularExpressionValidator ID="regexTextBox1"
ControlToValidate="YourTextBoxID" runat="server"
ValidationExpression="^[\s\S]{0,200}$" Text="200 characters max" />
You can avoid the use of custom validator altogether.
If you only need to force the max length of a text box field you have this at your disposal
<asp:TextBox MaxLength="200" id="name" runat=server> </asp:TextBox>

VB.net: Accessing data from DataList in Page_Load

New to VB.net, and trying to re-factor an 'old-skool' ASP page where all the page logic is happening on the .aspx page itself, to code-behind. Basically, I have a button that has a state, either on or off. If on, I set a hidden field to 1, if off, I set it to 0 (the default when a user visits the page).
The goal is to simply change the message I am sending to the user.
Have the following relevant code in MyPage.aspx:
<asp:HiddenField ID="hfldState" runat="server" Visible="false" Value="0" />
<div id="mainContent">
<asp:Literal ID="lblMessage" runat="server"
Visible="false" />
<asp:DataList ID="dlList" runat="server"
DataSourceID="sdsList"
DataKeyField="Entry No_"
RepeatLayout="Flow">
<ItemTemplate>
<div>
<asp:HyperLink ID="hlCurriculum" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "File Path") %>'
ToolTip='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
Target="_blank"
Style="font-weight: bold;">
</asp:HyperLink>
</div>
</ItemTemplate>
</asp:DataList>
</div>
<asp:SqlDataSource ID="sdsList" runat="server"
ConnectionString="..."
SelectCommand="SELECT [Entry No_], [Title], [File Path] FROM [Table] WHERE ([State] = #State)">
<SelectParameters>
<asp:ControlParameter ControlID="hfldState" Name="State" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
And the following in the Page_Load Sub:
If Page.IsPostBack Then
' Check for results
dlList.DataBind()
If dlList.Items.Count > 0 Then
' Results, display them
lblMessage.Text = "<h3>Results</h3>"
lblMessage.Visible = True
Else
' No results
lblMessage.Text = "<p>No Results</p>"
lblMessage.Visible = True
End If
Else
' user has not clicked anything
lblMessage.Text = "<p>Click button!</p>"
End If
The above code produces the following effect: when I click the button, setting the state to 1 and the page reloads, in Page_Load, I am getting zero results from the If dlList.Items.Count > 0 Then check, and thus am showing the 'No results' message, but the actual asp:DataList on the MyPage.aspx page is returning results... and then if I click the button again, setting it back to 0, in Page_Load, the datalist is now returning results, so I set the text to 'Results', but again, the control on the MyPage.aspx page behaves correctly, and shows no results, as expected. Now keep in mind that the above works perfectly in terms of checking the number of results if I move the If dlList.Items.Count > 0 Then page logic out of Page_Load and back to the MyPage.aspx file, it all works fine (i.e. results when state = 1, none when state = 0)
Any ideas?
I dont see a check in your page load for a postback:
If Not IsPostBack
'code
dList.DataBind() 'here is where you want to bind the data...
end if
The issue related to my ignorance of the page lifecycle in VB.net. Solution was to adjust the visibility of the asp:Literal, asp:DataList, set values etc in Page_PreRenderComplete. Basically, the asp:DataList wasn't being set in Page_Load, as expected, so doing any kind of logic in Page_Load based on the item count didn't make any sense.

Updateable Data Grid using Linq to SQL in WinForms

I'm populating a datagrid using Linq--standard kind of stuff (code below). For some reason however my ultraDataGrid is stuck in some kind of read-only mode. I've checked all the grid properties I can think of. Is that a result of binding to a Linq data source? Anyone have example code of an updatable grid that uses Linq?
db = New DataContext
myData = New dataClass
dataUltraGrid.DataSource = From table _
In db.profiles _
Select table.field1, table.field2...
Your really not using a LinqDataSource control... your are binding to a List db.profiles your data grid doesn't know anything about updating or deleting or inserting by just being bound to that list, can I suggets this:
<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSource1"
AutoGenerateColumns="False" DataKeyNames="FooID">
<Columns>
<asp:BoundField DataField="FooID" HeaderText="FooID" InsertVisible="False"
ReadOnly="True" SortExpression="FooID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="YourDataContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" TableName="Foos">
</asp:LinqDataSource>
Found the solution: use lambda expressions to filter the entity and bind directly to the entity.