This code below converts a Label to TextBox when clicked using jQuery in ASP.Net. But it doesn’t update the database.
However i am having problem identifying the text box, so that i can write some code inside the TextBox_TextChanged
This code works but, it is not what i need.
<div>
<table border="0" cellpadding="0" cellspacing="0" style="width: 222px">
<tr>
<td>Name: </td>
<td>
<asp:Label ID="lblName" runat="server" Text="LINGERS" CssClass="editable" />
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="Button1" Text="Submit" runat="server" OnClick="Submit" /></td>
</tr>
</table>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
//Loop through all Labels with class 'editable'.
$(".editable").each(function () {
//Reference the Label.
var label = $(this);
//Add a TextBox next to the Label.
label.after("<input type = 'text' style = 'display:none' />");
//Reference the TextBox.
var textbox = $(this).next();
//Set the name attribute of the TextBox.
var id = this.id.split('_')[this.id.split('_').length - 1];
textbox[0].name = id.replace("lbl", "txt");
//Assign the value of Label to TextBox.
textbox.val(label.html());
//When Label is clicked, hide Label and show TextBox.
label.click(function () {
$(this).hide();
$(this).next().show();
});
//When focus is lost from TextBox, hide TextBox and show Label.
textbox.focusout(function () {
$(this).hide();
$(this).prev().html($(this).val());
$(this).prev().show();
});
});
});
</script>
protected void Submit(object sender, EventArgs e)
{
string name = Request.Form["txtName"];
lblName.Text = name;
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Name: " + name + "');", true);
}
//what i want is on TextBox_TextChanged, This should happen assuming the Textbox is Textox1
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string updateSQL;
updateSQL = "update job set machine ='" + TextBox1.Text.ToString() + "' where pid='212' ";
dbConn.ConnectionString = WebConfigurationManager.ConnectionStrings["DB_SCH"].ConnectionString;
cmd.Connection = dbConn;
cmd.CommandText = selectSQL;
cmd.CommandType = CommandType.Text;
try
{
dbConn.Open();
int updated = cmd.ExecuteNonQuery();
if (updated == 1)
{
// Response.Redirect("changepassw.aspx");
}
else
{
// Label101.Text = "Process not Completed";
}
dbConn.Close();
}
catch (Exception err)
{
Response.Write(err.ToString());
}
finally
{
dbConn.Close();
}
}
Please help