Mas

Mas

  • NA
  • 478
  • 70.7k

How to show the grid on click event,It is showing default

Apr 8 2020 1:55 AM
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" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(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 type = 'hidden' />");
input.prop("question", question);
input.val(textbox.val());
row.find("td").eq(index).append(input);
//Clear the TextBox.
textbox.val("");
}
});
</script>
<style>
.content-text, #gvinterview{
font-family:'Segoe UI';
font-size:11px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="margin-left:50px;">
<table style="width:680px; height:200px;"> <%--border: 1px solid #ccc;--%>
<tr>
<td>
<asp:Label ID="lblquestions" runat="server" Font-Size="12px" Text="<b>Questions template title</b>"></asp:Label>
</td>
<td>
</td>
</tr>
<br />
<tr class="content-text"><td>
<span > Job-id:</span><span style="color:red;font-size:11px">*</span>
<br/>
<asp:TextBox ID="Txtjobid" runat="server" Width="95%" placeholder="Job id"></asp:TextBox>
</td>
<td></td>
</tr>
<tr class="content-text"><td>
<span > Job title:</span><span style="color:red;font-size:11px">*</span>
<br/>
<asp:TextBox ID="txtrolename" runat="server" Width="95%" placeholder="Type role name"></asp:TextBox>
</td><td></td></tr>
<tr class="content-text">
<td>
<span > Job description :</span><span style="color:red;font-size:11px">*</span>
<br /> <asp:TextBox ID="lbljobdescription" runat="server" Width="95%" Rows="6" ReadOnly="false" TextMode="MultiLine" placeholder="Type Job description" cssClass="content-text-black" TabIndex="2" ></asp:TextBox>
</td>
<td style="width:75px">
<asp:Button ID="Button1" runat="server" Text="Recomended questions" Width="150px" Height="30px" style="background-color:darkblue; color:white; font-family:'Segoe UI'; font-size:11px; border:1px solid #DCDCDC" />
</td>
</tr>
</table>
<table style="width:500px; height:200px;">
<tr >
<td style="font-family:'Segoe UI'; font-size:12px;">
<asp:Label ID="lblquestion" runat="server" Text="<b>Add question for above job description<b>" ></asp:Label>
</td>
</tr>
<tr class="content-text">
<td >
<asp:TextBox ID="txtquestion" runat="server" Width="100%" Text="" placeholder="Enter interview question"/>
</td></tr>
<tr class="content-text">
<td >
<asp:TextBox ID="txtanswer" runat="server" Width="100%" Text="" placeholder="Enter excepeted answer" Rows="6" ReadOnly="false" TextMode="MultiLine"/>
</td>
</tr>
<tr class="content-text">
<td >
<asp:Button ID="btnAdd" runat="server" Text="Add" Width="60px" style="background-color:darkblue; color:white; font-family:'Segoe UI'; font-size:11px; border:1px solid #DCDCDC" />
</td>
</tr>
</table>
<asp:GridView ID="gvinterview" Width="500px" CssClass = "table" runat="server" HeaderStyle-ForeColor="White" HeaderStyle-BackColor="#000099" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Question" ItemStyle-Width="150px" ItemStyle-CssClass="Question">
<ItemTemplate>
<%# Eval("Question") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer" ItemStyle-Width="310px" ItemStyle-CssClass="Answer">
<ItemTemplate>
<%# Eval("Answer")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="40px" >
<ItemTemplate>
<asp:ImageButton ID="btn_delete" ImageUrl="del.png" runat="server" style="width:25px;height:25px; align-content:center;" />
<%--<asp:Button ID="Btn_Delte" runat="server" Text="Delete" CommandName="Delete" />--%>
<%--<img id="btn_delete" src="del.png" style="width:25px;height:25px; align-content:center;" onclick="remove" />--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Submit" runat="server" OnClick="Submit" Width="60px" style="background-color:darkblue; color:white; font-family:'Segoe UI'; font-size:11px; border:1px solid #DCDCDC"/>
</div>
</form>
</body>
</html>
 C#
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);
}
}
}
}
 

Answers (1)