Dynamically add textbox if the database value is Text - asp.net

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.

Related

Create dropdownlist and two textbox each time when click button

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.

How to assign a default value to DetailsView in insert mode?

I tried to write a code to assign a value when I change the DetailsView to insert mode. for example, I need to assign an ID in a field:
1- I converted the field to (edit template).
2- I give it a new id.
3- I used this code in a Button_Click as below :
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DetailsView1.ChangeMode(DetailsViewMode.Insert)
Dim txt As TextBox
If DetailsView1.CurrentMode = DetailsViewMode.Insert Then
txtBox = DirectCast(DetailsView1.FindControl("TextBox1"), TextBox)
txtBox .Text = ViewState("932")
End If
End Sub
but when I clicked the button it gives me this error.
Object reference not set to an instance of an object.
any help please ?

How to access a control after Edit is clicked in asp:datagrid editcommand

I need to access a label control after the edit is clicked to bindgrid based on the label text. How do I do that?
Private Sub ActionItems_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles ActionItems.EditCommand
ActionItems.EditItemIndex = e.Item.ItemIndex
Dim fieldtypelbl As Label = e.Item.FindControl("lblrcause")
FillActions(fieldtypelbl.text)
End Sub
I was able to resolve the same by using
source.Parent.fieldname
as the datagrid is a part of the control being called on the same page multiple times

Persisting Dynamic ReadOnly Field Past Initialisation

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).

How to find selected GridView row in page_load event?

I have a page with GridView. The GridView has select button. Normally I use GridView's selected index changed event to do all kinds of operations when user clicks select button. But now I want to do some operations in Page_Load event based on grid view's selected row. Since the Selected_Index_changed event occurs after Page_Load how do I know following things in page load event.
I checked the asp lifecycle and this other question but I dont know how to do this.
How about using a QueryString to transmit which row was selected and then in the Page_Load event get the QueryString parameters? This is an example.
Protected Sub LinkButton1_Command(sender As Object, e As CommandEventArgs)
Dim UserId As Integer = e.CommandArgument 'Here goes whatever value you're trying to pass
Response.Redirect("~/OtherPage.aspx?UserId=" _
& UserId)
End Sub
This is in the OtherPage.aspx
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
UserId = Request.QueryString("UserId")
'Your code
end sub

Resources