如何激活按钮上的SqlDataSource点击,我有两个sql数据源,2个文本框,两个按钮和一个网格视图
我想将1 sqldatasource绑定到一个按钮点击,另一个按钮绑定到另一个按钮,例如。
GridView2.DataSource =SqlDataSource2;
GridView2.DataBind();
除了它不起作用,它不会输出任何东西。当我测试查询它工作得很好
CS#
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace IEIMS
{
public partial class Master : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click2(object sender, EventArgs e)
{
GridView1.DataSource = SqlDataSource2;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.DataSource = SqlDataSource3;
GridView1.DataBind();
}
}
}
asp代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Master.aspx.cs" Inherits="IEIMS.Master" %>
<!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>
</head>
<link href="Plugins/bootstrap-3.3.1-dist/dist/css/bootstrap-theme.min.css" rel="stylesheet"
type="text/css" />
<link href="Plugins/bootstrap-3.3.1-dist/dist/css/bootstrap.min.css" rel="stylesheet"
type="text/css" />
<link href="Plugins/login.css" rel="stylesheet" type="text/css" />
<script src="Plugins/jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="Plugins/bootstrap-3.3.1-dist/dist/js/bootstrap.min.js" type="text/javascript"></script>
<body>
<form id="form1" runat="server">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">IEIS</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">View <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="master.aspx">Master List</a></li>
<li><a href="Deployed.aspx">Inventory List</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Deploy <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="Account.aspx">Deploy Items</a></li>
<li><a href="Returned.aspx">Return Itmes</a></li>
<li class="divider"></li>
<li><a href="Add.aspx">Add Desktop/Laptop</a></li>
<li><a href="AddType2.aspx">Add Accessories</a></li>
<li><a href="additem.aspx">Add Model</a></li>
<li><a href="defective.aspx">Add Defective Items</a></li>
<li class="divider"></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Print<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="export.aspx">Export Master List</a></li>
<li><a href="Default.aspx">Print Accountability Form</a></li>
<li><a href="returnslip.aspx">Print Return Slip</a></li>
<li class="divider"></li>
</ul>
</li>
<li><a href="Login.aspx">Logout</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div>
</div>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Desktop</asp:ListItem>
<asp:ListItem>Laptop</asp:ListItem>
<asp:ListItem>Keyboard</asp:ListItem>
<asp:ListItem>Mouse</asp:ListItem>
<asp:ListItem>Avaya</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server"
Text="Search" onclick="Button1_Click2" Width="218px" />
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:MonitorConnectionString5 %>"
SelectCommand="SELECT * FROM [masterlist]"></asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MonitorConnectionString5 %>"
SelectCommand="SELECT * FROM [masterlist] WHERE ([itemname] = @itemname)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="itemname"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Search Serial Number" />
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:MonitorConnectionString5 %>"
SelectCommand="SELECT * FROM [masterlist] WHERE ([serialnumber] = @serialnumber)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="serialnumber"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CssClass="table" AllowSorting="True"
CellPadding="4" ForeColor="#333333" GridLines="None" >
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</form>
</body>
</html>
答案 0 :(得分:0)
那是因为您将AutoGenerateColumns
设置为false并且您没有明确地绑定列,所以如果您想自动绑定它,请将其设置为true
: -
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"