I am trying on every button click to add a drop-down list (filled from a database using EF) and two textboxes dynamically,
and after that store tha value from list and textbox into the database.
How can I do that using asp.nrt(vb) and EF?
First you will want to set up a Panel on the Client side to hold your new Textbox, and dropdownlists like below:
<asp:Panel ID="pnlButton" runat="server" Width="100%"/>
Then you will want to have a event set up like this for your button click.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tb As New TextBox
tb.ID = "TextBox1"
Dim ddl As New DropDownList
ddl.ID = "DropDownList1"
ddl.DataSource = 'your data source from the Database'
pnlButton.Controls.Add(tb)
pnlButton.Controls.Add(ddl)
End Sub
This should get you started on atleast creating the controls dynamically.
Related
I create a readonly textbox dynamically on page load/init and on the first load (not IsPostBack) I set the text. I also have a button on the page with a click event that changes the content of the textbox. Using another button, I read the text of the textbox. The problem is as follows:
If I click the button that changes the textbox and then click the button that reads the text of the textbox, it gets it fine.
If I just load the page and click the button that reads the text of the textbox, it brings back an empty string
I need both scenarios to bring back a result - whether it's the original text or the new programmatically changed text.
Example code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Textbox As New TextBox()
Textbox.ID = "Bob"
Textbox.ReadOnly = True
If Not IsPostBack Then
Textbox.Text = "Initial Text"
End If
Content.Controls.Add(Textbox)
Dim Button As New Button()
Button.Text = "Change text"
AddHandler Button.Click, AddressOf Button_Click
Content.Controls.Add(Button)
End Sub
Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
'FindControlRecursive is locally defined - just finds a control based on root control and ID
CType(FindControlRecursive(Content, "Bob"), TextBox).Text = "Hmmmmm"
End Sub
Private Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
Dim Textbox As TextBox = CType(FindControlRecursive(Content, "Bob"), TextBox)
MsgBox(Textbox.Text)
End Sub
I could just reinitialise the textbox each time but this is just an example and in practice I'll be pulling it from SQL and if I have several read only controls on the page I'd rather get the control to persist its text rather than going back to SQL each time.
I found two ways to solve this, the second being the more favoured approach (depending on situation):
Specifically add the initial text to the viewstate and then set the text on each postback using the value from viewstate:
If Not IsPostBack Then
Textbox.Text = "Initial Text"
ViewState("Bob") = Textbox.Text
Else
Textbox.Text = ViewState("Bob")
End If
Add a PreRender event to the textbox that just sets it text to itself. This takes place after the ViewState starts being tracked so it's added to the viewstate via the control and is persisted across postbacks:
If Not IsPostBack Then
Textbox.Text = "Initial Text"
AddHandler Textbox.PreRender, AddressOf PersistPastInitialLoad
End If
And add new sub:
Protected Sub PersistPastInitialLoad(ByVal sender As Object, ByVal e As EventArgs)
'This is just for example - would need refining for different types of controls
CType(sender, TextBox).Text = CType(sender, TextBox).Text
End Sub
Essentially they both do the same thing - hold the text in viewstate - but it depends on implementation which is the best to use. The first implementation might be a bit easier to read/work with but it would result in redundant viewstate bloat if you click the button to change the textbox text (i.e. you'll have the initial text in viewstate against the viewstate key you created as well as the current text added to viewstate by the control onchange).
I have an asp.net page written with vb.net that has a gridview table. The table is setup to have a checkbox column for each row. If this checkbox is checked for a row, I do some additional things. One of the other columns also includes a hyperlink on the text for each cell in that column.
I have a button on the page that will loop through and programmatically check each one of the checkboxes. The button does this correctly; however, it also removes the hyperlink from the other column for some reason. Any ideas why?
Here is the code where I set the hyperlinks for the hyperlink column:
Protected Sub gridData_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridData.RowDataBound
'When a row is added to the data grid view we need to add the hyperlink to the zipname column
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cell As TableCell = e.Row.Cells(4)
Dim lnk As HyperLink = New HyperLink
'Set properties of the link
lnk.NavigateUrl = cell.Text
lnk.Text = cell.Text
'Format the filename, filepath, zipname, and username to be lowercase
e.Row.Cells(1).Text = LCase(e.Row.Cells(1).Text)
e.Row.Cells(3).Text = LCase(e.Row.Cells(3).Text)
e.Row.Cells(4).Text = LCase(e.Row.Cells(4).Text)
e.Row.Cells(5).Text = LCase(e.Row.Cells(5).Text)
'Add the hyperlink
cell.Controls.Clear()
cell.Controls.Add(lnk)
End If
End Sub
Here is the code for the check all button:
Protected Sub cmdCheckAll_Click(sender As Object, e As EventArgs) Handles cmdCheckAll.Click
'Check all of the download checkboxes
For Each row As GridViewRow In gridData.Rows
CType(row.FindControl("CheckBox1"), CheckBox).Checked = True
Next
End Sub
As is the usual case with Dynamic Controls, All the dynamically created Hyperlinks in RowDataBound event needs to be recreated every postback.
So, there are 2 options.
1.) Either bind your GridView on every postback :
protected void Page_Load(object sender, EventArgs e)
{
gridData.DataBind();
}
OR
2.) Turn off ViewState for your GridView::
<asp:GridView ID="gridData" EnableViewState="false" ... />.
On using any of above options, the OnRowDataBound event is executed each time postback happens, thus recreating the Dynamic controls. Now you can access the Hyperlinks in your button click event.
Check properly which one of above options is best for your application.
I have a database field call "type" (TextBox, DropDown, CheckBox) and "value" column. I need to read the type and display in the webpage dynamically using VB.net.
Example: If the database type is TextBox I need to display a TextBox in the webpage and when user enters a value in the TextBox, this needs to update to the database field "value".
How do I dynamically add textbox if the database value is Text in vb.net?
Try this. It will check the value of MyDatabaseType which you should get from your database. If the value is "TextBox", it will create a TextBox and add it to the form MyForm:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
If MyDatabaseType = "TextBox" Then
Dim TextBox1 As New TextBox
TextBox1.ID = "TextBox1"
MyForm.Controls.Add(TextBox1)
End If
InitializeComponent()
End Sub
For more details, you can check this article from Microsoft.
Good afternoon people have been trying to fill a dropdown using a sql command, until so good, when I click on the dropdown it shows all the items, but when I try to click on an item in the dropdown it always returns the first item in the dropdown .... follows the codes, what i want to do is get the selected value and item from the dropdown and save it on a label for future use.
I appreciate all the support possible,
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
utilizador.Text = Me.Context.User.Identity.Name
If (Not Page.IsPostBack) Then
escolhePerfil()
End If
'DropDownPerfil.DataBind()
lbperfil2.Text = DropDownPerfil.SelectedItem.Text
lbnome.Text = DropDownPerfil.SelectedValue
End Sub
Function escolhePerfil() As Boolean
Dim connstring As String = "Data Source=10.2.24.17;Persist Security Info=True;User ID=sa;Password=Pr0dUn1C0$qL;database=ePrimavera"
Dim SQLData As New System.Data.SqlClient.SqlConnection(connstring)
Dim cmdSelect As New System.Data.SqlClient.SqlCommand("SELECT u.WindowsUser,u.Email ,g.Description, u.Login FROM [ePrimavera].[dbo].[PLT_Users] as u,[ePrimavera].[dbo].[PLT_UserGroups] as ug, [ePrimavera].[dbo].[PLT_Groups] as g where u.ID = ug.UserID And ug.GroupID = g.ID and u.WindowsUser like 'bancounico\" & utilizador.Text & "'", SQLData)
SQLData.Open()
Dim dtrReader As System.Data.SqlClient.SqlDataReader = cmdSelect.ExecuteReader()
If dtrReader.HasRows Then
DropDownPerfil.DataValueField = "Login"
DropDownPerfil.DataTextField = "Description"
DropDownPerfil.DataSource = dtrReader
DropDownPerfil.DataBind()
End If
SQLData.Close()
Return True
End Function
.aspx
<asp:DropDownList ID="DropDownPerfil" runat="server"
Height="16px" Width="202px" CssClass="DropBorderColor">
</asp:DropDownList>
try the following code:
Protected Sub DropDownPerfil_SelectedIndexChanged(sender As Object, e As EventArgs)
lbperfil2.Text = DropDownPerfil.SelectedItem.Text
lbnome.Text = DropDownPerfil.SelectedValue
End Sub
The problem is that you are trying to get the value "too early". The value is not valid in the Page_Load, since the control's OnLoad event fired after the Page's OnLoad (Page_Load) event.
The way, what the others wrote the correct, since the Event handlig section (inclueding control's onchanged event) will process after the Load section.
For more details check the offical ASP.NET life cycle site in the MSDN
The likely reason is that some (if not all) the values you are assigning to the DropDownList for Login are occur more than once.
When you then select an item in the DDL, if the value of that item occurs more than once, the selected index will highlight the first instance. To test this, comment out DropDownPerfil.DataValueField = "Login"
I am sure upon selection, it will highlight the correct item you intended on selecting.
I have a a gridview attached to an objectdatasource. In each row I have a bound textbox for input. I have a button beside the textbox in each row that launches a javascript popup for unit conversion.
The question is: how can i tell the unit converter (js function) what textbox (in which row) to populate the results with?
In the RowCreated event of the GridView:
Protected Sub MyGridView_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btn As Button = e.Row.Cells(0).FindControl("btnJavaScriptButton")
Dim txt As TextBox = e.Row.Cells(1).FindControl("txtResults")
btn.OnClientClick = "calculate(" & txt.ClientID & ");"
End If
End Sub
Where 0 and 1 are the indices of the columns that contain the button and the textbox, and "calculate" is the name of your JavaScript function.