开发者

vb.net ideas on how to populate all text boxes and make them visible once a button is clicked

开发者 https://www.devze.com 2023-02-16 01:53 出处:网络
I was looking for some better way of coding to populate all text boxes and make them visible once a button is clicked, is there a better way of doing this than I describe below?

I was looking for some better way of coding to populate all text boxes and make them visible once a button is clicked, is there a better way of doing this than I describe below?

The scenario

I have a letter like format on a page with labels displaying address, name etc and it was recommended that on this page some of the information should be able to be edited. The idea I had was to keep the labels displaying the information but then when the edit details button is clicked the labels disappear, text boxes will appear and be populated with the same text as the label to allow the user to edit the details.

I seem to repeating lines of code but changing one thing each time

on page load

Dim Sname As String = Request.QueryString("Name")
lblStuName.Text = Sname
tbStuName.Text = Sname
tbStuName.visible=false

Dim Saddress As String = Request.QueryString("Address")
lblAddress.Text = Saddress
tbAddress.Text = Saddress
tbAddress.visible = false

etc...

on edit button

tbStuName.visible=true
tbAddress.visible = true

etc..

on the save button

 sqlStatement = "UPDATE StuTable SET Name = '" & lblStuName开发者_运维技巧.Text & "', Address = '" & lblAddress.Text

My code seems very repetitive in many parts, I was looking for a better coding style to do this can anyone suggest a better way to do it? Is my idea sound right way to do this or can anyone suggest a better way it can be implemented?

Thank you


Have you considered activating the inputs on the client-side? You could use a jquery plugin such as this http://www.appelsiini.net/projects/jeditable

You'd still have to do some setup in your VB though.


You could code a method to do the dirty work and then just call it. You could even have a list of the textboxes:

Private _txtBoxes as Generic.List(Of TextBox)
Private Sub DoSetup(value as string, txt as TextBox, lbl as Label)
  txt.Text = value
  lbl.Text = value
  txt.Visible = false
  _txtBoxes.Add(txt)
End Sub

Call (on page load):

DoSetup(Request.Querystring("Name"),txtStuName,lblStuName)
DoSetup(Request.Querystring("Address"),txtAddress,lblAddress)

Setting to visible (on edit button):

For Each txt as TextBox in _txtBoxes
   txt.Visible = True
Next


An alternative strategy would be to use javascript in the webform to switch the controls over:

In page section:

<script lang="javascript">
   function DoEditClick() {
      document.getElementById("txtStuName").style.visibility = "visible";
      document.getElementById("lblStuName").style.visibility = "hidden";
      document.getElementById("txtAddress").style.visibility = "visible";
      document.getElementById("lblAddress").style.visibility = "hidden";
      ...
   }
</script>

The markup for the edit button would look something like this:

<asp:Button runat="server" id="btnEdit" text="Edit" OnClientClick="DoEditClick()"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消