Sir,
I have an textbox at top and below Gridview get binded on the basis of phone number passed from textbox. If phone number already exixts in record then it would fetch and bind the record in grid therefore textbox's at footer get visible to add new records. But if phone number is new then no data received on binding and therefore footer template is not visble to enter new records.
I want if data is available or not the grid's footer template should always get visible. How should I do this?
HTML:
<form id="form1" runat="server">
<center>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox3" runat="server" AutoPostBack="True" OnTextChanged="TextBox3_TextChanged" MaxLength="10"></asp:TextBox>
<asp:GridView ID="GridViewEmployee" runat="server" EmptyDataText="No record." AutoGenerateColumns="False" ShowFooter="True" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Visible="false" Text='<%# Server.HtmlEncode (Eval("Id").ToString()) %>'/>
<asp:CheckBox ID="CheckBox1" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ControlStyle-CssClass="centerAlign">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Server.HtmlEncode (Eval("name").ToString()) %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="nameTextbox" runat="server"/>
</FooterTemplate>
<ControlStyle CssClass="centerAlign" />
</asp:TemplateField >
<asp:TemplateField HeaderText="Age" ControlStyle-CssClass="centerAlign">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Server.HtmlEncode (Eval("age").ToString()) %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ageTextbox" runat="server"/>
</FooterTemplate>
<ControlStyle CssClass="centerAlign" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" ControlStyle-CssClass="centerAlign">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Server.HtmlEncode (Eval("address2").ToString()) %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="addressTextbox" runat="server"/>
</FooterTemplate>
<ControlStyle CssClass="centerAlign" />
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" Text="Add New Nominee" OnClick="Button1_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Submit" />
<br />
<asp:Label ID="Label4" runat="server"></asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SLIConnectionString %>" SelectCommand="SELECT * FROM [customer] WHERE ([phone_no] = @phone_no)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox3" Name="phone_no" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridViewEmployee" />
<asp:AsyncPostBackTrigger ControlID="TextBox3" EventName="TextChanged" />
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</center>
</form>
c#:
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SLIConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack == true)
{
//SetInitialRow();
}
}
private void AddNewRecord(string phoneno, string employee, string position, string team)
{
string sqlStatement = "INSERT INTO customer" +
"(phone_no,name,age,address2)" +
"VALUES (@phoneno,@name,@age,@address2)";
try
{
SqlCommand cmd = new SqlCommand(sqlStatement, connection);
cmd.Parameters.AddWithValue("@phoneno", phoneno);
cmd.Parameters.AddWithValue("@name", employee);
cmd.Parameters.AddWithValue("@age", position);
cmd.Parameters.AddWithValue("@address2", team);
cmd.CommandType = CommandType.Text;
connection.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
connection.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Extract the TextBoxes that is located under the footer template
TextBox tbEmployee = (TextBox)GridViewEmployee.FooterRow.Cells[0].FindControl("nameTextbox");
TextBox tbPosition = (TextBox)GridViewEmployee.FooterRow.Cells[1].FindControl("ageTextbox");
TextBox tbTeam = (TextBox)GridViewEmployee.FooterRow.Cells[2].FindControl("addressTextbox");
//call the method for adding new records to database and pass the necessary parameters
AddNewRecord(TextBox3.Text.Trim(), tbEmployee.Text, tbPosition.Text, tbTeam.Text);
//Re-Bind the GridView to reflect the changes made
//BindGridView(TextBox3.Text.Trim());
GridViewEmployee.DataBind();
}
private void BindGridView(string s)
{
DataTable dt = new DataTable();
//SqlConnection connection = new SqlConnection(GetConnectionString());
try
{
connection.Open();
string sqlStatement = "SELECT [id], [name], [age], [address2] FROM customer WHERE phone_no = @phone_no";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
sqlCmd.Parameters.AddWithValue("@phone_no", s);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridViewEmployee.DataSource = dt;
GridViewEmployee.DataBind();
}
}
catch (Exception ex)
{
}
finally
{
connection.Close();
}
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
//Gridview1.Visible = true;
////SetInitialRow();
//Gridview1.DataBind();
//BindGridView(TextBox3.Text.Trim());
GridViewEmployee.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
//Response.Write("<script>alert('hello');</script>");
try
{
string str = String.Empty;
string name = String.Empty;
string age = String.Empty;
string addr = String.Empty;
foreach (GridViewRow gvrow in GridViewEmployee.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
if (chk != null & chk.Checked)
{
Label lblname = (Label)gvrow.FindControl("Label1");
Label lblage = (Label)gvrow.FindControl("Label2");
Label lbladdr = (Label)gvrow.FindControl("Label3");
str += "<b>Name : </b>" + lblname.Text + ", ";
str += "<b>Age : </b>" + lblage.Text + ", ";
str += "<b>Address : </b>" + lbladdr.Text+"<br/>";
str += "=========================================<br/>";
}
}
Label4.Text = str;
}
catch (Exception)
{
}
finally
{
connection.Close();
}
}