how to get the User control Value which is nested inside the datagrid to String

本小妞迷上赌 提交于 2019-12-25 03:56:16

问题


I need to access the Value of User control which is present inside the data Grid.as shown below

Actually what i want is once the data is bind to the grid it will check whether there is a value in the Expence column of the Datagrid if the value is null then the user control will be visible where user can select the value and will click save. my aspx code:

<asp:UpdatePanel ID="UPChargesGrid" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DataGrid ID="dgDestination" runat="server" BorderWidth="1px" BorderColor="#FE9B00"
        BorderStyle="Solid" BackColor="White" Font-Names="Verdana" Font-Size="XX-Small AutoGenerateColumns="False" ShowFooter="FALSE" CellPadding="3"       align="center" Width="700px" OnItemCommand="dgDestination_Select"
                     <Columns>
                         <asp:BoundColumn DataField="mCHR_NUPKId" HeaderText="mFDD_mFRD_NUPKId" Visible="False"></asp:BoundColumn>
                         <asp:BoundColumn DataField="Income" HeaderText="Income"></asp:BoundColumn>
                         <asp:TemplateColumn HeaderText="Expence">
                              <ItemTemplate>
                                   <asp:Label ID="LBLEXcepceValue" runat="server" Text='<%# Eval("Expence") %>' > </asp:Label>
                                    <CC5:Charge ID="ChargeExpence" runat="server" />         
                              </ItemTemplate>
                        </asp:TemplateColumn>
                        <asp:TemplateColumn HeaderText="SAVE">
                             ItemTemplate>
                                   <asp:ImageButton runat="server" ID="IMGBTNSave" ImageUrl="~/AppImages/save.png"  CommandName="Save" />
                              </ItemTemplate>
                       </asp:TemplateColumn>
                    </Columns>
                    <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"> </PagerStyle>
                 </asp:DataGrid>
         </contenttemplate>
</asp:UpdatePanel>  

my CS code:

  protected void dgDestination_Select(Object sender, DataGridCommandEventArgs e)
{
    string expence = "";
    UPGridControls.Update();
    if (e.CommandName == "Save")
        {
           string FRDID = e.Item.Cells[0].Text;
           string income =  e.Item.Cells[1].Text;   
            if (FRDID != "")
             {
        Label LBLEXcepceValue = (Label)dgDestination.Items[vLoop].FindControl("LBLEXcepceValue");

                  if (LBLEXcepceValue.Text != income)
                    expence = (dgDestination.FindControl("ChargeExpence") as UserControl).ToString();
         using (LQMasterChargesMappingDataContext DB = new LQMasterChargesMappingDataContext())
                 {
                    var newFRD = new TB_MasterChargesMapping
                    {
                         mCHM_mCHR_NUPKId_Income =mobjGenlib.ConvertLong( GetChargeID(e.Item.Cells[1].Text.ToString())),
                         mCHM_mCHR_NUPKId_Expences = mobjGenlib.ConvertLong(GetChargeID(expence)),
                         mCHM_mCMP_NUUniqueId = mobjGenlib.ConvertLong(TXTCompanyID.Text),
                         mCHM_NUIsActive = 1
                    };
                    DB.TB_MasterChargesMappings.InsertOnSubmit(newFRD);
                    DB.SubmitChanges();

                    B_MasterChargesMapping MASLEVPREMAP = DB.TB_MasterChargesMappings.OrderByDescending(C => C.mCHM_NUPKId).Where(A => A.mCHM_NUIsActive == 1 && A.mCHM_mCMP_NUUniqueId == mobjGenlib.ConvertLong(TXTCompanyID.Text)).FirstOrDefault();
                  //Insert In UserLog Report
                     mobjGenlib.InsertUserlog("mCHM", mobjGenlib.ConvertString(MASLEVPREMAP.mCHM_NUPKId), "I");
                    TB_MasterChargesMapping NewFRD = (from P in DB.TB_MasterChargesMappings where P.mCHM_mCHR_NUPKId_Income == mobjGenlib.ConvertLong(GetChargeID(e.Item.Cells[1].Text.ToString())) && P.mCHM_NUIsActive == 1 select P).FirstOrDefault();
                    if (NewFRD != null)
                    {
                      Session["ODDID"] = mobjGenlib.ConvertString(NewFRD.mCHM_NUPKId);
                }
                 }
             }
       }
}

please help me to et the value from user control to String. Thanks in advance.


回答1:


You can access your expense control like this

var expense= e.Item.FindControl("ChargeExpence") as yourUserControl;

You can define a public property in your user control, which actually gives you the value. You can then access this property using expense object retrieved above.




回答2:


You can access the control id which you want to access inside a user control. Try this.

 if (expence != null){
      //access controls here that are within yout usercontrol for example
      TextBox tb = (TextBox)expence.FinControl("YourTextBoxID");
string requiredValue = tb.Text;

    }


来源:https://stackoverflow.com/questions/25445020/how-to-get-the-user-control-value-which-is-nested-inside-the-datagrid-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!