Having difficulty with asp:gridview - c#

I seem to be having difficulty with asp:querystringparameter and asp:gridview. I have the following so far and it just returns "no data available":
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
String strGroup = Request.QueryString["group"];
switch(strGroup){
case "Clients":
ClientSource.SelectCommand = #"select client_code,
client_name from table1 where client_name = #phrase";
break;
case "Addresses":
/*different query here*/
break;
case "Matters":
/*different query here*/
break;
default:
break;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family:Arial;">
<asp:gridview id="ClientGridView"
datasourceid="ClientSource"
emptydatatext="No data available."
runat="server">
</asp:gridview>
<asp:SqlDataSource id="ClientSource"
runat="server"
ConnectionString="connection string goes here">
<SelectParameters>
<asp:QueryStringParameter Type="String" Name="phrase" QueryStringField="phrase" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>

you can bind gridview dynamically in code behind also. Try to debug if your query is returning any data. Is there any specific reason to use SqlDataSource?

Related

asp.net display textbox on item checked

So just starting out with asp.net... I want to display my textbox when my checkbox is checked, but this doesn't seem to be working. I also tried with the visible property, but that didn't work either. What am I doing wrong exactly?
Code:
protected void checked_CheckedChanged(object sender, EventArgs e)
{
text.Style["display"] = "block";
}
Layout:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<p>gehuwd/samenwonend<asp:checkbox runat="server" ID="checked" OnCheckedChanged="checked_CheckedChanged"></asp:checkbox>
</p>
<asp:TextBox runat="server" ID="text" style="display:none"></asp:TextBox>
</form>
</body>
</html>
Use the AutoPostBack property for checkbox and set it to true:
<asp:checkbox runat="server" ID="checked" OnCheckedChanged="checked_CheckedChanged" AutoPostBack="true"></asp:checkbox>
You can use add css property of textbox in c# as given below. If your checkbox OnCheckedChanged is not working then you can set property AutoPostBack is true in checkbox.
protected void checked_CheckedChanged(object sender, EventArgs e)
{
text.Attributes.Add("display","block");
}
You can also do this completely client side, using jQuery or javascript.Making a post back to the server everytime you need to change the visual appearance of your HTML can put unnecessary strain on the server and have a negative effect on the user experience by slowing down the overall performance of your site.
<head runat="server">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var id = "<%: text.ClientID %>";
id = "#" + id;
$(id).hide();
$("#chkShowHide").change(function () {
if (this.checked) {
$(id).show();
}
else{
$(id).hide();
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<input id="chkShowHide" type="checkbox" /> Show\Hide<br />
<asp:TextBox runat="server" ID="text"></asp:TextBox>
</form>
</body>

Performing both client side and server side functions on button click - Specific Scenario

I have a checkbox, label and button control.
If the checkbox is not checked and button is clicked, I need to display a message in label stating to check the checkbox first.
If the checkbox is checked and then the button clicked, it should allow me to proceed.
This is very similar to the terms and conditions screens,where if you dont check the checkbox - you are not allowed to proceed.
I am using the below javascript. Please let me know how do I accomplish this functionality?
<script type="text/javascript">
function testCheckbox() {
var obj = document.getElementById('<%= chkTerms.ClientID %>');
if (obj.checked == false) {
document.getElementById("lblCheck").style.visibility = "visible";
return false;
}
}
</script>
<asp:Label ID="lblTerms" runat="server" Text="I agree to the Terms and Conditions"> </asp:Label>
<asp:Label ID="lblCheck" runat="server" Text="Please agree to the terms and conditions to proceed"></asp:Label>
<asp:Button ID="btnProceed" runat="server" OnClientClick ="return testCheckbox()" OnClick="btnProceed_Click" Text="Submit" />
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="js/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#lblCheck').hide();
$('#btnProceed').click(function () {
var $this = $('#chkTerms')
if ($this.is(':checked')) {
$('#lblCheck').hide();
return true;
} else {
$('#lblCheck').show();
return false;
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBox ID="chkTerms" runat="server" Text="I agree to the Terms and Conditions"/><br />
<asp:Label ID="lblCheck" runat="server" Text="Please agree to the terms and conditions to proceed"/><br />
<asp:Button ID="btnProceed" runat="server" Text="Submit" onclick="btnProceed_Click1" />
</form>
</body>
</html>
Code behind:
protected void btnProceed_Click1(object sender, EventArgs e)
{
Response.Write("DD");
//your proceed
}

Session variable lost when click sign in

In first visit, Page_Load is running and session store correctly. When I click on Sign In button, Page_Load is call again, and then call btnSignIn_Click function, but Session is empty!
public partial class LoginPage : System.Web.UI.Page
{
UserItem userItem = null;
protected void Page_Load(object sender, EventArgs e)
{
//Initialize and validate post
RequestObj post = new RequestObj(Context);
if (post.isValid)
{
Session["post"] = post;
}
}
protected void btnSignIn_Click(object sender, EventArgs e)
{
if (Session["post"] != null)
{
RequestObj post = Session["post"] as RequestObj;
userItem = Functions.LogIn(post);
}
Response.Redirect("LogIn.aspx");
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<%# Page Language="C#" AutoEventWireup="true" CodeBehind=" LoginPage.aspx.cs"
Inherits="myNameSpace.LoginPage " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="viewport" content="width=320"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:100%; text-align:center;">
<%if (userItem == null)
{%>
Username:
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<br />
Password:
<asp:TextBox ID="txtPass" runat="server" TextMode="Password">
</asp:TextBox>
<br />
<asp:Button ID="btnSignIn" runat="server" Text="Sign In"
onclick="btnSignIn_Click" />
<%}else{%>
<%=userItem.LoginName%>
<br />
<%=userItem.LoginTime.ToString()%>
<br />
<asp:Button ID="btnSignOut" runat="server" Text="Sign Out"
onclick="btnSignOut_Click" />
<%}%>
</div>
</form>
</body>
</html>
Did you mean to only set session when the page renderes initially?
Try wrapping the assignment like this
if(!IsPostBack)
{
RequestObj post = new RequestObj(Context);
if (post.isValid)
{
Session["post"] = post;
}
}
This will prevent this from being reset during a postback
Otherwise I would check the value of post in a debugger

Ajax toggle button extender not working

<asp:CheckBox ID="CheckBox1" OnCheckedChanged="CheckBox1_CheckedChanged" runat="server" Text="Toggle Visibility" AutoPostBack="true"/>
and an Ajax toggle button extander:
<ajaxToolkit:ToggleButtonExtender ID="Ext1" CheckedImageAlternateText="View" TargetControlID="CheckBox1" runat="server" CheckedImageUrl="~/Images/Others/view.png" UncheckedImageAlternateText="Hide" UncheckedImageUrl="~/Images/Others/hide.png" ImageWidth="32" ImageHeight="24" ></ajaxToolkit:ToggleButtonExtender>
But it is not working.
The page is loading but with the default checkboxes.
Thanks for any help
you can try like this.....
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
CheckBox myCB = new CheckBox();
myCB.ID = "myCB";
myCB.Checked = true;
myCB.Text = "I like ASP.NET";
Panel1.Controls.Add(myCB);
AjaxControlToolkit.ToggleButtonExtender myTBE = new AjaxControlToolkit.ToggleButtonExtender();
myTBE.TargetControlID = "myCB";
myTBE.CheckedImageUrl = "ToggleButton_Checked.gif";
myTBE.UncheckedImageUrl = "ToggleButton_Unchecked.gif";
myTBE.ImageHeight = 19;
myTBE.ImageWidth = 19;
Panel1.Controls.Add(myTBE);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>
</body>
</html>

javascript is not working in mozilla but working in other browers

Technology Used:- Asp.Net 2.0
Code:- See Below
Description:- hello code given below is working fine in i.e. and other but not working in all mozila version.javascript is simple to devide two textbox's value. you can easily understand.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="javascript_test.aspx.cs" Inherits="javascript_test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script type ="text/jscript">
var _txtamount;
var _txtins;
var _txtinsamount;
function test()
{
var temp;
_txtamount = document.getElementById("txtamount");
_txtins = document.getElementById("txtins");
_txtinsamount = document.getElementById("txtinsamount");
if (_txtinsamount.value !='')
{
temp = parseFloat(_txtamount.value) / parseFloat(_txtinsamount.value);
}
else
{
temp = 0
}
_txtins.value = temp;
}
</script>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="myform" runat="server" name="myform">
<div>
<asp:TextBox ID="txtamount" runat="server" ></asp:TextBox>
<asp:TextBox ID="txtinsamount" runat="server" onblur="test();"></asp:TextBox>
<asp:TextBox ID="txtins" runat="server"></asp:TextBox></div>
</form>
</body>
</html>
You're using text/jscript as the <script> type. Use text/javascript instead:
<script type ="text/javascript">
JScript is Microsoft's own version of ECMAScript -- no wonder it works on IE.
Your <script> tag needs to be wrapped in the <head> element or in the <body> element; it can't just be a direct child of <html>.
[edit]more important is your "type" value, as the other answer here mentions.

Categories