DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
DropDownList d4 = GridView1.FooterRow.FindControl("DropDownList4") as DropDownList;
DropDownList d6 = GridView1.FooterRow.FindControl("DropDownList6") as DropDownList;
DropDownList d8 = GridView1.FooterRow.FindControl("DropDownList8") as DropDownList;
DropDownList d10 = GridView1.FooterRow.FindControl("DropDownList10") as DropDownList;
SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBe"].DefaultValue = d8.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBc"].DefaultValue = d10.SelectedValue;
SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
SqlDataSource1.Insert();如上,我想以此实现从footer插入功能,但是上面的代码出错!提示信息如下:
"
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 30: SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue;
行 31: SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
行 32: SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue;
"
小弟愚笨,求达人指点,实在不明白错在哪里了。
为空了!
if(d2.selectitem!=null)
SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue;
if(d4.selectitem!=null)
SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
if(d6.selectitem!=null)
SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue;
这样不知道对不
得试下了
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
Response.Write(d2.SelectedValue);
}
它可以输出我想要的内容。。
大致如下:楼主试试,结贴的时候再来看看我的记忆力怎样DropDownList d3 = (DropDownList)GridView1.Controls[0].Controls[0].FindControl("DropDownList2");
if(ds2!=null)
{
// do sth
}其余同上
是寻找<EmptyDataTemplate>模板列中控件的!!
改成GridView1.FooterRow.Cell[4].FindControl("DropDownList2") as DropDownList
4是DropDownList2所在的单元格列号
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class doctor_yiganliangduiban : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void LinkButton3_Click(object sender, EventArgs e)
{
DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
DropDownList d4 = GridView1.FooterRow.FindControl("DropDownList4") as DropDownList;
DropDownList d6 = GridView1.FooterRow.FindControl("DropDownList6") as DropDownList;
DropDownList d8 = GridView1.FooterRow.FindControl("DropDownList8") as DropDownList;
DropDownList d10 = GridView1.FooterRow.FindControl("DropDownList10") as DropDownList;
SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBe"].DefaultValue = d8.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBc"].DefaultValue = d10.SelectedValue;
SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
SqlDataSource1.Insert();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
//DropDownList d1 = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
//DropDownList d3 = GridView1.FooterRow.FindControl("DropDownList3") as DropDownList;
//DropDownList d5 = GridView1.FooterRow.FindControl("DropDownList5") as DropDownList;
//DropDownList d7 = GridView1.FooterRow.FindControl("DropDownList7") as DropDownList;
//DropDownList d9 = GridView1.FooterRow.FindControl("DropDownList9") as DropDownList;
//// [HBsAg] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
////[AntiHBs] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
////[HBeAg] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
////[AntiHBe] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
////[AntiHBc] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL
//SqlDataSource1.UpdateParameters["HBsAg"].Name = d1.SelectedValue; ////SqlDataSource1.UpdateParameters["HBsAg"].DefaultValue = d1.SelectedValue;
////SqlDataSource1.UpdateParameters["AntiHBs"].DefaultValue = d3.SelectedValue;
////SqlDataSource1.UpdateParameters["HBeAg"].DefaultValue = d5.SelectedValue;
////SqlDataSource1.UpdateParameters["AntiHBe"].DefaultValue = d7.SelectedValue;
////SqlDataSource1.UpdateParameters["AntiHBc"].DefaultValue = d9.SelectedValue; ////SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
////SqlDataSource1.Insert();
//SqlDataSource1.Update();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
Table gvTable = ((Table)GridView1.Controls[0]);
string d15 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList15")).SelectedValue;
string d11 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList11")).SelectedValue;
string d12 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList12")).SelectedValue;
string d13 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList13")).SelectedValue;
string d14 = ((DropDownList)gvTable.Rows[0].FindControl("DropDownList14")).SelectedValue;
SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d15;
SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue=d11;
SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d12;
SqlDataSource1.InsertParameters["AntiHBe"].DefaultValue =d13;
SqlDataSource1.InsertParameters["AntiHBc"].DefaultValue =d14;
SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
SqlDataSource1.Insert();
//GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
Response.Write(d2.SelectedValue);
}
}
这是所有的代码了。
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width: 474px; height: 118px">
<tr>
<td colspan="3">
促红素</td>
</tr>
<tr>
<td colspan="3" style="height: 408px; vertical-align: top;">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1" ShowFooter="True" AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="处方日期" SortExpression="checkTime">
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("checkTime", "{0:D}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("checkTime", "{0:D}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="促红素每周剂量(IU)" SortExpression="cuhongsujiliang">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("cuhongsujiliang") %>' MaxLength="5" ValidationGroup="up"></asp:TextBox><asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="粗红素剂量只能输入数字!" ValidationExpression="[-]{0,1}\d+\.\d+|[1-9]\d*" ValidationGroup="up">*</asp:RegularExpressionValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox2" runat="server" MaxLength="5" ValidationGroup="up"></asp:TextBox><asp:RegularExpressionValidator
ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="粗红素剂量只能输入数字!" ValidationExpression="[-]{0,1}\d+\.\d+|[1-9]\d*" ValidationGroup="up">*</asp:RegularExpressionValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("cuhongsujiliang") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="更新" ValidationGroup="up"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">保存</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" OnClientClick="return confirm('删除后将无法恢复,是否确认删除此行信息?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E3EAEB" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
<EmptyDataTemplate>
<table style="width: 309px">
<tr>
<td style="font-weight: bold; width: 25px; color: white; background-color: #1c5e55; height: 6px;">
<nobr>处方日期</nobr></td>
<td style="font-weight: bold; width: 156px; color: white; background-color: #1c5e55; height: 6px;">
<nobr>促红素每周剂量(IU)</nobr></td>
<td style="font-weight: bold; width: 34px; color: white; background-color: #1c5e55; height: 6px;">
<nobr>操作</nobr></td>
</tr>
<tr>
<td style="width: 25px; height: 18px;">
</td>
<td style="width: 156px; height: 18px;">
<nobr>
<asp:TextBox ID="TextBox3" runat="server" ValidationGroup="eb"></asp:TextBox><asp:RegularExpressionValidator
ID="RegularExpressionValidator3" runat="server" ControlToValidate="TextBox3"
ErrorMessage="粗红素剂量只能输入数字!" ValidationExpression="[-]{0,1}\d+\.\d+|[1-9]\d*" ValidationGroup="eb">*</asp:RegularExpressionValidator></nobr></td>
<td style="width: 34px; height: 18px;">
<nobr><asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click" ValidationGroup="eb">保存</asp:LinkButton></nobr></td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyDataRowStyle BackColor="#E3EAEB" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:XYJHConnectionString %>"
DeleteCommand="DELETE FROM cuhongsu WHERE (ID = @id)" InsertCommand="INSERT INTO cuhongsu(cuhongsujiliang, UserID) VALUES (@cuhongsujiliang, @userid)"
SelectCommand="SELECT [cuhongsujiliang], [checkTime], [ID], [UserID] FROM [cuhongsu] where userid=@userid"
UpdateCommand="UPDATE cuhongsu SET cuhongsujiliang = @cuhongsujiliang WHERE (ID = @id)">
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="cuhongsujiliang" />
<asp:Parameter Name="id" />
</UpdateParameters>
<SelectParameters>
<asp:SessionParameter Name="userid" SessionField="userid" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="cuhongsujiliang" />
<asp:Parameter Name="userid" />
</InsertParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
</asp:Content>
这个LinkButton3在哪里的??
void Button1_Click(object sender, EventArgs e)
{
TextBox t= Gridview1.FooterRow.FindControl("TextBox1) as TextBox;
SqlDataSource1.InsertParameters["ID"].DefaultValue = t.Text;
SqlDataSource1.Insert();
}foreach(GridViewRow gr in this.GridView1.Rows)
{}
可以将你的前台SqlDataSource代码贴一下!!
由于当表里没有内容的时候所有的footer不显示,所有我在EmptyDataTemplate模板里也插入一个两行表格,在表格第二行插入dropdownlist控件,第一行填写相应字段名,最后面插入一个linkbutton,点击linkbutton,实施insert语句
INSERT INTO yiganliangduiban(UserID, HBsAg, AntiHBs, HBeAg, AntiHBe, AntiHBc) VALUES (@UserID, @HBsAg, @AntiHBs, @HBeAg, @AntiHBe, @AntiHBe)
EmptyDataTemplate模板中用
Controls[0].Controls[0].FindControl.....
找控件!!
<InsertParameters>
<asp:Parameter Name="UserID" />
<asp:Parameter Name="HBsAg" />
........................
</InsertParameters>
对吗?
还有你那个UserID是自增长的还是其他??
DropDownList d2 = GridView1.FooterRow.FindControl("DropDownList2") as DropDownList;
DropDownList d4 = GridView1.FooterRow.FindControl("DropDownList4") as DropDownList;
.........仔细看了主贴,出错好象是设置insert参数出错!
SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
出一样的问题,即使我把等号左边改成常量,也是一样的问题。
所以我想是不是在赋值的时候出现啥问题了。。
SqlDataSource1.InsertParameters["HBsAg"].DefaultValue = d2.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBs"].DefaultValue = d4.SelectedValue;
SqlDataSource1.InsertParameters["HBeAg"].DefaultValue = d6.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBe"].DefaultValue = d8.SelectedValue;
SqlDataSource1.InsertParameters["AntiHBc"].DefaultValue = d10.SelectedValue;
SqlDataSource1.InsertParameters["userid"].DefaultValue = Session["userid"].ToString();
如果传常量都不可以的话!那可能是设置SqlDataSource1哪儿有问题!
要不对数据库你没有Insert权限??
建议:
另外建一个页面,最简单弄两个字段试试,究竟问题在哪儿?
因为我过去和你类似的搞过,没什么问题!!