Hello Memebers,
Hope you are doing good!!
Can any one guide me on below issue, Below I am adding the code also.
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="tquestion.WebForm1" %>
$(function () {
$("[id*=gvinterview] [id*=btn_delete]").click(function () {
alert("Deleted");
$(this).closest("tr").remove();
return false;
} )
});
$(function () {
$("[id*=btnAdd]").click(function () {
//Reference the GridView.
var gridView = $("[id*=gvinterview]");
//Reference the first row.
var row = gridView.find("tr").eq(1);
//Check if row is dummy, if yes then remove.
if ($.trim(row.find("td").eq(0).html()) == "") {
row.remove();
}
//Clone the reference first row.
row = row.clone(true);
//Add the Name value to first cell.
var txtquestion = $("[id*=txtquestion]");
SetValue(row, 0, "question", txtquestion);
//Add the Country value to second cell.
var txtanswer = $("[id*=txtanswer]");
SetValue(row, 1, "answer", txtanswer);
//Add the row to the GridView.
gridView.append(row);
return false;
});
function SetValue(row, index, question, textbox) {
//Reference the Cell and set the value.
row.find("td").eq(index).html(textbox.val());
//Create and add a Hidden Field to send value to server.
var input = $("");
input.prop("question", question);
input.val(textbox.val());
row.find("td").eq(index).append(input);
//Clear the TextBox.
textbox.val("");
}
});
.content-text, #gvinterview{
font-family:'Segoe UI';
font-size:11px;
}
Job-id:* | |
Job title:* | |
Job description :* |
<%# Eval("Question") %>
<%# Eval("Answer")%>
<%-- --%>
<%--
--%>
--%>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
namespace tquestion
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
string query = "SELECT question, answer FROM interview";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
{
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
}
if (dt.Rows.Count == 0)
{
//If no records then add a dummy row.
dt.Rows.Add();
}
gvinterview.DataSource = dt;
gvinterview.DataBind();
}
protected void Submit(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.Form["question"]) && !string.IsNullOrEmpty(Request.Form["answer"]))
{
//Fetch the Hidden Field values from the Request.Form collection.
string[] questions= Request.Form["question"].Split(',');
string[] answers = Request.Form["answer"].Split(',');
//Loop through the values and insert into database table.
for (int i = 0; i < questions.Length; i++)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO interview VALUES(@question, @answer)"))
{
cmd.Parameters.AddWithValue("@question", questions[i]);
cmd.Parameters.AddWithValue("@answer", answers[i]);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
//Refresh the page to load GridView with records from database table.
Response.Redirect(Request.Url.AbsoluteUri);
}
}
}
}
Jignesh KumarPosted Apr 8, 2020, 2:15 AM