ImageButton未获取GridView单元格值

时间:2015-09-02 16:28:07

标签: c# asp.net gridview imagebutton detailsview

代码之前正在运行,我所做的就是在gridview的标题模板中添加一个新的文本框。

那我的代码在做什么:

我的gridview视图上有一个图像按钮,单击时我获取gridview的当前行的值和单元格3的值,并将其设置为等于我的文本框。然后基于该值我使用模态弹出扩展器使两个细节视图可见,一个细节视图正确呈现但不会起作用,除非它从textbox10.text获取值作为参数,另一个细节视图甚至不会显示详细信息,因为select查询将textbox10.text作为参数。

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (!IsPostBack)
{
}
else
{
ImageButton imgbtn = (ImageButton)sender;
GridViewRow GridView1 = (GridViewRow)imgbtn.NamingContainer;
string workordernum = GridView1.Cells[3].Text;

TextBox10.Text = workordernum;

//TextBox10.Text = "1";

ModalPopupExtender2.Show();
DetailsView2.Visible = true;
ModalPopupExtender1.Show();
DetailsView1.Visible = true;
}
}

当我将上述代码置于调试模式时: enter image description here

调试完成后

enter image description here

当我设置textbox10.text = 1并运行应用程序时:

enter image description here

完成具有值1的应用程序:

enter image description here

详细信息视图的

.aspx代码:

<asp:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="pnl2" PopupControlID="pnl2" BackgroundCssClass="modalBackground"  DropShadow="False" X="30" Y="300" ValidateRequestMode="Inherit"></asp:ModalPopupExtender>

<asp:HiddenField ID="HiddenField2" runat="server" />

<asp:Panel ID="pnl2" runat="server" Width="881px" Height="175px" CssClass="pnl2BackGround" >
<asp:DetailsView ID="DetailsView2" runat="server" DataSourceID="SqlDataSource3" BackColor="#593B03"  Height="175px" Width="881px" Font-Bold="True" ForeColor="White" PostBack = "False" AutoGenerateRows="False" DataKeyNames="WorkOrderNum" >
<Fields>
<asp:BoundField DataField="WorkOrderNum" HeaderText="WorkOrderNum" InsertVisible="False" ReadOnly="True" SortExpression="WorkOrderNum" />
<asp:BoundField DataField="Requestor" HeaderText="Requestor" SortExpression="Requestor" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" DataFormatString="{0:dd-M-yyyy}" />
<asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
<asp:BoundField DataField="CompletionDate" HeaderText="CompletionDate" SortExpression="CompletionDate" DataFormatString="{0:dd-M-yyyy}" />
<asp:BoundField DataField="MachineDescription" HeaderText="MachineDescription" SortExpression="MachineDescription" />
<asp:BoundField DataField="MachineLocation" HeaderText="MachineLocation" SortExpression="MachineLocation" />
<asp:BoundField DataField="Type_of_Work_Order" HeaderText="Type_of_Work_Order" SortExpression="Type_of_Work_Order" />
<asp:BoundField DataField="Work_Required" HeaderText="Work_Required" SortExpression="Work_Required" />
</Fields>
</asp:DetailsView>
</asp:Panel>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:WorkOrderConnectionString6 %>" SelectCommand="SELECT [WorkOrderNum], [Requestor], [Date], [Department], [CompletionDate], [MachineDescription], [MachineLocation], [Type of Work Order] AS Type_of_Work_Order, [Work Required] AS Work_Required FROM [Master] WHERE ([WorkOrderNum] = @WorkOrderNum)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox10" Name="WorkOrderNum" PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

我用于从标题模板中搜索gridview的代码:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Filter"] = "All";
DetailsView1.Visible = false;
TextBox10.Visible = false;
DetailsView2.Visible = false;
DetailsView3.Visible = false;
ReportViewer1.Visible = false;
BindGrid();

}

}

private void BindGrid()
{
DataTable dt = new DataTable();
String WorkOrderConnectionString = System.Configuration.ConfigurationManager
.ConnectionStrings["WorkOrderConnectionString3"].ConnectionString;
SqlConnection con = new SqlConnection(WorkOrderConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("spx_GetWorkOrderNum");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
TextBox TextBox92 = (TextBox)GridView1.HeaderRow.FindControl("WorkOrderNum");


}
protected void TextBox92_TextChanged(object sender, EventArgs e)
{
TextBox TextBox92 = (TextBox)sender;
ViewState["Filter"] = TextBox92.Text;
this.BindGrid();
}

我怀疑回帖可能是问题,有些地方在代码中它正在做一个不经常的回复所以当我点击图像按钮时它没有得到值

请帮我找到我的代码中的错误,因为调试器没有帮助我,而且这是以前的工作代码:(

为什么TEXTBOX VALUE =“”不列颠哥伦比亚省的细胞价值?????? 感谢

1 个答案:

答案 0 :(得分:0)

由于Gridview是一个Control而TextBox也是Gridview控件中的一个Control,我们需要这样做:

 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

  {
   ImageButton imgbtn = (ImageButton)sender;
   GridViewRow GridView1Row = (GridViewRow)imgbtn.NamingContainer;
   Label FLabel = GridView1Row.Cells[2].Controls[0].FindControl("Label1") as Label;
                string workordernum = FLabel.Text;// GridView1Row.Cells[3].Text;

                TextBox10.Text = workordernum;

                    ModalPopupExtender2.Show();
                    DetailsView2.Visible = true;
                    ModalPopupExtender1.Show();
                    DetailsView1.Visible = true;



        }