Use of data binding

Source: Internet
Author: User
Tags count integer
Data if you want to assign data values to a control property at run time, you can respond to data-binding events that are raised by the control. In the event code, you can perform any logic regardless of whether the logic is required to allocate the property value.
Note At design time, you can create a data-binding expression that evaluates at run time to assign a value to a control property. For more information, see Data Binding Order Value Web server controls at design time.
Data binding order value control at run time
1. Make sure that the code in your Web forms page or call the DataBind method of the control you are using, or call the method for this page. A data-binding event is raised in response to this method call.
Note It is not usually necessary to call the DataBind method in each round trip (that is, the postback is not required to be checked during page initialization) because doing so replaces the value in the control.
2. Create an event handler for the DataBinding event of the control.
3. In the event handler, add code to assign the value to the control's properties as needed.
The following example shows how to perform a simple data binding at run time. The Web Forms page contains two TextBox Web server controls that display information from the Header table in the SQL Server pubs database. The previous and Next buttons set the record location variable stored in session state. The DataBind method of the page is invoked whenever the user clicks the buttons to change the location of the record, which raises the event used to populate the text box.
' Visual Basic
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill (DSTITLES1)
Session ("Recordpos") = 0
Me.databind ()
End Sub

Private Sub Btnnext_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnnext.click
Dim Recordpos as Integer = CType (Session ("Recordpos"), integer)
Recordpos + 1
If recordpos > DsTitles1.titles.Count Then
Recordpos-= 1
End If
Session ("Recordpos") = Recordpos
Me.databind ()
End Sub

Private Sub Btnprevious_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnprevious.click
Dim Recordpos as Integer = CType (Session ("Recordpos"), integer)
If recordpos > 0 Then
Recordpos-= 1
End If
Session ("Recordpos") = Recordpos
Me.databind ()
End Sub

Private Sub textbox1_databinding (ByVal sender as Object, ByVal e as System.EventArgs) Handles textbox1.databinding
Dim Recordpos as Integer = CType (Session ("Recordpos"), integer)
TextBox1.Text = Dstitles1.titles (recordpos). title_id
TextBox2.Text = Dstitles1.titles (recordpos). Title
End Sub

C#
private void Page_Load (object sender, System.EventArgs e)
{
SqlDataAdapter1.Fill (DSTITLES1);
session["Recordpos"] = 0;
This. DataBind ();
}

private void Btnnext_click (object sender, System.EventArgs e)
{
int recordpos = (int) session["Recordpos"];
recordpos++;
if (Recordpos > DsTitles1.titles.Count)
{
recordpos--;
}
session["Recordpos"] = Recordpos;
This. DataBind ();
}

private void Btnprevious_click (object sender, System.EventArgs e)
{
int recordpos = (int) session["Recordpos"];
if (Recordpos > 0)
{
recordpos--;
}
session["Recordpos"] = Recordpos;
This. DataBind ();
}

private void Textbox1_databinding (object sender, System.EventArgs e)
{
int recordpos = (int) session["Recordpos"];
TextBox1.Text = dstitles1.titles[recordpos].title_id;
TextBox2.Text = Dstitles1.titles[recordpos].title;
}
The following example binds a Label control to the value returned by executing the SQL command. The Command object (which is explicit for SQL Server, indicated by a named parameter) has previously been defined by a single parameter named @empid.
The user enters the employee ID and clicks the button. This button invokes the DataBind method of the Label control, which raises the DataBinding event. In an event handler, the code gets the employee ID, sets it as a parameter to the SqlCommand object, and runs the SQL statement for the command. This command returns a data reader object that has a record that the code reads and displays in the label.
' Visual Basic
Private Sub Btnlogin_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnlogin.click
Label1.databind ()
End Sub

Private Sub label1_databinding (ByVal sender as Object, ByVal e as System.EventArgs) Handles label1.databinding
Sqlcommand1.parameters ("@empid"). Value = Loginid.text
Sqlcommand1.commandtext = _
"SELECT ID, fname, lname from employee WHERE (id = @empid)"
Sqlcommand1.commandtype = CommandType.Text
Dim Dr as System.Data.SqlClient.SqlDataReader
Sqlconnection1.open ()
Dr = Sqlcommand1.executereader ()
If Dr. Read () Then
Label1.Text = "Welcome," & CType (DR (1), String) & "" & CType (DR (2), String)
Else
Label1.Text = "No employee found with ID of" & loginid.text
End If
Sqlconnection1.close ()
End Sub

C#
private void Btnlogin_click (object sender, System.EventArgs e)
{
Label1.databind ();
}

private void Label1_databinding (object sender, System.EventArgs e)
{
sqlcommand1.parameters["@empid"]. Value = Loginid.text;
Sqlcommand1.commandtext =
"SELECT ID, fname, lname from employee WHERE (id = @empid)";
Sqlcommand1.commandtype = CommandType.Text;
System.Data.SqlClient.SqlDataReader Dr;
Sqlconnection1.open ();
Dr = Sqlcommand1.executereader ();
if (Dr. Read ())
{
Label1.Text = "Welcome," + dr[1] + "" + dr[2];
}
Else
{
Label1.Text = "No employee found with ID of" + loginid.text;
}
Sqlconnection1.close ();
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.