我想在gridview中访问子网格文本框的值 [英] I want to access values of sub grid text box in gridview
问题描述
我创建了一个主要的gridview作为gvMainTest
并在其模板字段中创建了subgridview(gvSubTest)
我将数据绑定到Grid和sub gridview我有一个包含一些值的textbox字段现在我想要更新那些值
提交按钮当我使用foreach循环获取整个sub的值时gridview但我得到的值是文本框双打
我也有javascript函数在图像上显示隐藏子gridview但当我在子gridview上输入值然后隐藏子gridview在这种情况下,我需要将文本框的值插入到数据库中
我尝试过:
Gridview代码如下
< div class =row my-colstyle =border :1px solid#a0e3a2; margin:10px 0px; padding:5px 0px; runat =serverid =subentrygriddiv>
< div class =col-md-12 grid-view-divstyle =margin-top:10px;>
< div class =table-responsive grid-containerstyle =padding-bottom:20px; margin-left:0px;>
<% - < asp:GridView ID =gvCustomersrunat =serverAutoGenerateColumns =falseCssClass =GridDataKeyNames =Main_Test_IdOnRowDataBound =OnRowDataBound> - %> ;
< asp:GridView ID =gvMainTestrunat =serverAutoGenerateColumns =falseDataKeyNames =Main_Test_IdOnRowDataBound =gvMainTest_RowDataBoundCssClass =table table-borderedStyle =margin:0pxShowHeaderWhenEmpty = 真 >
< Columns>
< asp:TemplateField ItemStyle-Width =50pxItemStyle-HorizontalAlign =center>
< ItemTemplate>
< asp:CheckBox ID =CheckMainrunat =server/>
<% - < img alt =style =cursor:pointersrc =Images / zoom_out.png/> - %>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField ItemStyle-Width =50pxItemStyle-HorizontalAlign =center>
< ItemTemplate>
< img alt =style =cursor:pointersrc =images / zoom_out.png/>
<% - < asp:Panel ID =pnlOrdersrunat =serverStyle =display:none> - %>
< asp:Panel ID =pnlSubTestrunat =serverStyle =display:none>
< asp:GridView ID =gvSubTestrunat =serverAutoGenerateColumns =falseCssClass =table table-borderedOnRowDataBound =gvSubTest_RowDataBound>
<% - < asp:GridView ID =gvOrdersrunat =serverAutoGenerateColumns =falseCssClass =ChildGrid> - %>
< Columns>
< asp:TemplateField>
< ItemTemplate>
< asp:CheckBox ID =checkSubrunat =server/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:BoundField ItemStyle-Width =150pxDataField =Parameters_IdHeaderText =Parameters IdHeaderStyle-CssClass =hideItemStyle-CssClass =hidden/>
< asp:BoundField ItemStyle-Width =150pxDataField =Main_Test_IdHeaderText =Main Test IdHeaderStyle-CssClass =hideItemStyle-CssClass =hidden/>
< asp:BoundField ItemStyle-Width =400pxDataField =Parameter_NameHeaderText =Parameter Name/>
<% - < asp:BoundField ItemStyle-Width =200pxDataField =Result_ValHeaderText =Site | Customer | General/>
< asp:BoundField ItemStyle-Width =100pxDataField =Result_ValHeaderText =Min | Max/> - %>
< asp:BoundField ItemStyle-Width =200pxDataField =CustSiteGeneralRateSubHeaderText =Site | Customer | General/>
< asp:BoundField ItemStyle-Width =100pxDataField =MinMaxSubHeaderText =Min | Max/>
< asp:TemplateField>
< HeaderTemplate>
价格:
< / HeaderTemplate>
< ItemTemplate>
<% - < asp:TextBox ID =txtSubRateEnableViewState =falserunat =serverStyle =height:auto; padding-bottom:0px; margin:0px; OnTextChanged =txtSubRate_TextChangedOnLoad =txtSubRate_LoadAutoPostBack =true/> - %>
< asp:TextBox ID =txtSubRaterunat =serverStyle =height:auto; padding-bottom:0px; margin:0px; Text ='<%#Eval(Rate)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
<% - < asp:TemplateField>
< ItemTemplate>
< asp:TextBox ID =Valuerunat =serverStyle =height:auto; padding-bottom:0px; margin:0px; />
< / ItemTemplate>
< / asp:TemplateField> - %>
< / Columns>
< / asp:GridView>
< / asp:Panel>
< / ItemTemplate>
< / asp:TemplateField>
< asp:BoundField ItemStyle-Width =50pxDataField =Main_Test_IdHeaderText =Main_Test_IdHeaderStyle-CssClass =hideItemStyle-CssClass =hidden/>
< asp:BoundField ItemStyle-Width =500pxDataField =Main_Test_NameHeaderText =主要测试名称/>
< asp:BoundField ItemStyle-Width =100pxDataField =Department_nameHeaderText =Department Name/>
< asp:BoundField ItemStyle-Width =200pxDataField =CustSiteGeneralRateHeaderText =Site | Customer | General/>
< asp:BoundField ItemStyle-Width =100pxDataField =MinMaxHeaderText =Min | Max/>
< asp:TemplateField>
< HeaderTemplate>
价格:
< / HeaderTemplate>
< ItemTemplate>
< asp:TextBox ID =txtRaterunat =serverStyle =height:auto; padding-bottom:0px; margin:0px; Text ='<%#Eval(Rate)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField>
< HeaderTemplate>
是组:
< / HeaderTemplate>
< ItemTemplate>
< asp:CheckBox ID =IsGrouprunat =serverChecked =trueOnCheckedChanged =IsGroup_CheckedChanged/>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< / asp:GridView>
< / div>
< / div>
< / div>
< div class =row my-col-btn>
< div class =col-md-12>
< asp:LinkButton runat =serverID =btnSubmitValidationGroup =valCssClass =btn btn-successOnClick =btnSubmit_Click>提交< / asp:LinkButton>
< asp:LinkButton runat =serverID =btnUpdateValidationGroup =valCssClass =btn btn-successOnClick =btnUpdate_Click> Update< / asp:LinkButton>
< asp:LinkButton runat =serverID =btnResetCssClass =btn btn-dangerOnClick =btnReset_Click>重置< / asp:LinkButton>
< / div>
< / div>
javascript函数
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/ jquery.min.js>< /脚本>
< script type =text / javascript>
$([src * = zoom_out])。live(click,function(){
// alert(放大);
$(this).closest (< tr>< td>< / td>< td>'999'>+ $(this).next()。html()+< / td>< / tr>)
$(this).attr(src,images / zoom_in.png);
});
$([src * = zoom_in])。live(click,function(){
// alert(Zoom Out);
// alert(Test2 );
$(this).attr(src,images / zoom_out.png);
$(this).closest(tr)。next()。remove() ;
});
< / script>
btn提交代码如下
protected void btnSubmit_Click(object sender,EventArgs e)
{
int customerId = 0;
int SiteDetailsId = 0;
int RefFromPickUPReqest = 0;
int.TryParse(ddlCustomer.SelectedValue,out customerId);
int.TryParse(ddlSite.SelectedValue,out SiteDetailsId);
int.TryParse(ddlRefFromPickUPReqest.SelectedValue,out RefFromPickUPReqest);
Performa_invoice objPerforma_invoice = new Performa_invoice(-1,customerId,RefFromPickUPReqest,SiteDetailsId,DateTime.Now,EmpId);
if(objPerforma_invoice.Insert())
{
int PerformanceId = 0;
PerformanceId = objPerforma_invoice.GetMaxId();
int MainTestId = 0;
string checkBoxval =;
foreach(GridViewRow dr in gvMainTest.Rows)
{
CheckBox CheckMain = dr.FindControl(CheckMain)作为CheckBox;
if(CheckMain.Checked == true)
{
int.TryParse(dr.Cells [1] .Text,out MainTestId);
CheckBox chkItem =(CheckBox)dr.FindControl(IsGroup);
if(chkItem.Checked)
{
checkBoxval =Yes;
}
其他
{
checkBoxval =否;
}
TextBox txtRate =(dr.Cells [6]。.FindControl(txtRate)as TextBox);
// TextBox txtRate =(TextBox)Page.FindControl(txtRate);
Pi_main_test objpi_main_test = new Pi_main_test(-1,PerformanceId,MainTestId,dr.Cells [4] .Text,dr.Cells [5] .Text,txtRate.Text,checkBoxval);
// Pi_main_test objpi_main_test = new Pi_main_test(-1,PerformanceId,MainTestId,dr.Cells [4] .Text,dr.Cells [5] .Text,dr.Cells [6] .Text,checkBoxval);
objpi_main_test.Insert();
int PI_MT_ID = 0,ParameterId = 0;
PI_MT_ID = objpi_main_test.GetMaxId();
GridView gvSubTest = dr.FindControl(gvSubTest)作为GridView;
foreach(gvSubTest.Rows中的GridViewRow dr2)
{
int.TryParse(dr2.Cells [1] .Text,out ParameterId);
CheckBox checkSub = dr2.FindControl(checkSub)作为CheckBox;
if(checkSub.Checked == true)
{
TextBox txtparameterRate12 =(dr2.Cells [6] .FindControl(txtSubRate)as TextBox);
TextBox txtparameterRate22 = dr2.FindControl(txtSubRate)as TextBox;
// txtparameterRate12。
Pi_sub_test objPi_sub_test = new Pi_sub_test(-1,PI_MT_ID,MainTestId,ParameterId,dr2.Cells [4] .Text,dr2.Cells [5] .Text,txtparameterRate22.Text);
// Pi_sub_test objPi_sub_test = new Pi_sub_test(-1,PI_MT_ID,MainTestId,ParameterId,dr2.Cells [4] .Text,dr2.Cells [5] .Text,dr2.Cells [6] .Text);
objPi_sub_test.Insert();
}
}
}
}
AlertMesg(true,记录插入成功。);
重置();
PanelShowHide();
LoadGridView(1);
LoadDrowpDown();
}
else
{
AlertMesg(false,记录插入失败。请再试一次......!);
}
}
([src * = zoom_out])。live (click,function(){
// alert(放大);
(this).closest(tr)。after(< tr>< td>< / td>< td colspan ='999'>+
(this).next()。html()+< / TD>< / TR>中)
I have Created one main gridview as gvMainTest
and in its template field created subgridview (gvSubTest)
I have bind data to both Grid and sub gridview I have one textbox field which contain some values Now I want to update that values
on submit button when I Get values using foreach loop for entire sub gridview but i am getting values to textbox doubles
also I have javascript function to show hide sub gridview on image but when I Enter values on sub gridview and then hide sub gridview in that case I need that values of text box to insert into database
What I have tried:
Gridview code is as follows
<div class="row my-col" style="border: 1px solid #a0e3a2; margin: 10px 0px; padding: 5px 0px;" runat="server" id="subentrygriddiv"> <div class="col-md-12 grid-view-div" style="margin-top: 10px;"> <div class="table-responsive grid-container" style="padding-bottom: 20px; margin-left: 0px;"> <%--<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid" DataKeyNames="Main_Test_Id" OnRowDataBound="OnRowDataBound">--%> <asp:GridView ID="gvMainTest" runat="server" AutoGenerateColumns="false" DataKeyNames="Main_Test_Id" OnRowDataBound="gvMainTest_RowDataBound " CssClass="table table-bordered" Style="margin: 0px" ShowHeaderWhenEmpty="true"> <Columns> <asp:TemplateField ItemStyle-Width="50px" ItemStyle-HorizontalAlign="center"> <ItemTemplate> <asp:CheckBox ID="CheckMain" runat="server" /> <%--<img alt="" style="cursor: pointer" src="Images/zoom_out.png" />--%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-Width="50px" ItemStyle-HorizontalAlign="center"> <ItemTemplate> <img alt="" style="cursor: pointer" src="images/zoom_out.png" /> <%-- <asp:Panel ID="pnlOrders" runat="server" Style="display: none">--%> <asp:Panel ID="pnlSubTest" runat="server" Style="display: none"> <asp:GridView ID="gvSubTest" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered" OnRowDataBound="gvSubTest_RowDataBound" > <%-- <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">--%> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="checkSub" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField ItemStyle-Width="150px" DataField="Parameters_Id" HeaderText="Parameters Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hidden" /> <asp:BoundField ItemStyle-Width="150px" DataField="Main_Test_Id" HeaderText="Main Test Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hidden" /> <asp:BoundField ItemStyle-Width="400px" DataField="Parameter_Name" HeaderText="Parameter Name" /> <%-- <asp:BoundField ItemStyle-Width="200px" DataField="Result_Val" HeaderText="Site | Customer | General" /> <asp:BoundField ItemStyle-Width="100px" DataField="Result_Val" HeaderText="Min | Max" />--%> <asp:BoundField ItemStyle-Width="200px" DataField="CustSiteGeneralRateSub" HeaderText="Site | Customer | General" /> <asp:BoundField ItemStyle-Width="100px" DataField="MinMaxSub" HeaderText="Min | Max" /> <asp:TemplateField> <HeaderTemplate> Rate: </HeaderTemplate> <ItemTemplate> <%--<asp:TextBox ID="txtSubRate" EnableViewState="false" runat="server" Style="height: auto; padding-bottom: 0px; margin: 0px;" OnTextChanged="txtSubRate_TextChanged" OnLoad="txtSubRate_Load" AutoPostBack="true"/>--%> <asp:TextBox ID="txtSubRate" runat="server" Style="height: auto; padding-bottom: 0px; margin: 0px;" Text='<%# Eval("Rate") %>' /> </ItemTemplate> </asp:TemplateField> <%--<asp:TemplateField> <ItemTemplate> <asp:TextBox ID="Value" runat="server" Style="height: auto; padding-bottom: 0px; margin: 0px;" /> </ItemTemplate> </asp:TemplateField>--%> </Columns> </asp:GridView> </asp:Panel> </ItemTemplate> </asp:TemplateField> <asp:BoundField ItemStyle-Width="50px" DataField="Main_Test_Id" HeaderText="Main_Test_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hidden" /> <asp:BoundField ItemStyle-Width="500px" DataField="Main_Test_Name" HeaderText="Main Test Name" /> <asp:BoundField ItemStyle-Width="100px" DataField="Department_name" HeaderText="Department Name" /> <asp:BoundField ItemStyle-Width="200px" DataField="CustSiteGeneralRate" HeaderText="Site | Customer | General" /> <asp:BoundField ItemStyle-Width="100px" DataField="MinMax" HeaderText="Min | Max" /> <asp:TemplateField> <HeaderTemplate> Rate: </HeaderTemplate> <ItemTemplate> <asp:TextBox ID="txtRate" runat="server" Style="height: auto; padding-bottom: 0px; margin: 0px;" Text='<%# Eval("Rate") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <HeaderTemplate> Is Group: </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="IsGroup" runat="server" Checked="true" OnCheckedChanged="IsGroup_CheckedChanged" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </div> </div> <div class="row my-col-btn"> <div class="col-md-12"> <asp:LinkButton runat="server" ID="btnSubmit" ValidationGroup="val" CssClass="btn btn-success" OnClick="btnSubmit_Click">Submit</asp:LinkButton> <asp:LinkButton runat="server" ID="btnUpdate" ValidationGroup="val" CssClass="btn btn-success" OnClick="btnUpdate_Click">Update</asp:LinkButton> <asp:LinkButton runat="server" ID="btnReset" CssClass="btn btn-danger" OnClick="btnReset_Click">Reset</asp:LinkButton> </div> </div>
javascript function
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $("[src*=zoom_out]").live("click", function () { //alert("Zoom In"); $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>") $(this).attr("src", "images/zoom_in.png"); }); $("[src*=zoom_in]").live("click", function () { //alert("Zoom Out"); //alert("Test2"); $(this).attr("src", "images/zoom_out.png"); $(this).closest("tr").next().remove(); }); </script>
btn submit code is as follows
protected void btnSubmit_Click(object sender, EventArgs e) { int customerId = 0; int SiteDetailsId = 0; int RefFromPickUPReqest = 0; int.TryParse(ddlCustomer.SelectedValue, out customerId); int.TryParse(ddlSite.SelectedValue, out SiteDetailsId); int.TryParse(ddlRefFromPickUPReqest.SelectedValue, out RefFromPickUPReqest); Performa_invoice objPerforma_invoice = new Performa_invoice(-1, customerId, RefFromPickUPReqest, SiteDetailsId, DateTime.Now, EmpId); if (objPerforma_invoice.Insert()) { int PerformanceId = 0; PerformanceId = objPerforma_invoice.GetMaxId(); int MainTestId = 0; string checkBoxval = ""; foreach (GridViewRow dr in gvMainTest.Rows) { CheckBox CheckMain = dr.FindControl("CheckMain") as CheckBox; if (CheckMain.Checked == true) { int.TryParse(dr.Cells[1].Text, out MainTestId); CheckBox chkItem = (CheckBox)dr.FindControl("IsGroup"); if (chkItem.Checked) { checkBoxval = "Yes"; } else { checkBoxval = "No"; } TextBox txtRate = (dr.Cells[6].FindControl("txtRate") as TextBox); //TextBox txtRate = (TextBox)Page.FindControl("txtRate"); Pi_main_test objpi_main_test = new Pi_main_test(-1, PerformanceId, MainTestId, dr.Cells[4].Text, dr.Cells[5].Text, txtRate.Text, checkBoxval); //Pi_main_test objpi_main_test = new Pi_main_test(-1, PerformanceId, MainTestId, dr.Cells[4].Text, dr.Cells[5].Text, dr.Cells[6].Text, checkBoxval); objpi_main_test.Insert(); int PI_MT_ID = 0, ParameterId = 0; PI_MT_ID = objpi_main_test.GetMaxId(); GridView gvSubTest = dr.FindControl("gvSubTest") as GridView; foreach (GridViewRow dr2 in gvSubTest.Rows) { int.TryParse(dr2.Cells[1].Text, out ParameterId); CheckBox checkSub = dr2.FindControl("checkSub") as CheckBox; if (checkSub.Checked == true) { TextBox txtparameterRate12 = (dr2.Cells[6].FindControl("txtSubRate") as TextBox); TextBox txtparameterRate22 = dr2.FindControl("txtSubRate") as TextBox; //txtparameterRate12. Pi_sub_test objPi_sub_test = new Pi_sub_test(-1, PI_MT_ID, MainTestId, ParameterId, dr2.Cells[4].Text, dr2.Cells[5].Text, txtparameterRate22.Text); //Pi_sub_test objPi_sub_test = new Pi_sub_test(-1, PI_MT_ID, MainTestId, ParameterId, dr2.Cells[4].Text, dr2.Cells[5].Text, dr2.Cells[6].Text); objPi_sub_test.Insert(); } } } } AlertMesg(true, "Record insert successfully."); Reset(); PanelShowHide(); LoadGridView(1); LoadDrowpDown(); } else { AlertMesg(false, "Record insertion fail. Please try again...!"); } }
("[src*=zoom_out]").live("click", function () { //alert("Zoom In");
(this).closest("tr").after("<tr><td></td><td colspan = '999'>" +
(this).next().html() + "</td></tr>")
这篇关于我想在gridview中访问子网格文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!